3

AH! I have a list of strings... and i just want to remove an element from the list if it is ""

but i keep causing the program to crash. How do i get around this? I made a list of arrays into a List of strings thinking i could remove it that way. Here is what i have:

        List<String>list;

        String[] f= new String[file.length()];
        f= file.split("<"); 

        list= Arrays.asList(f); 

        final Iterator<String> iter = list.iterator();
        while (iter.hasNext()) 
        {
            final String temp = iter.next();
            // TODO check for zero-length arrays
            if (temp.equals("")) 
            {
                iter.remove();
            }
        }

Error:

Exception in thread "main" java.lang.UnsupportedOperationException
    at java.util.AbstractList.remove(Unknown Source)
        at java.util.AbstractList$Itr.remove(Unknown Source)
        at utilities.Validation.Validate(Validation.java:44)

After i convert it to a List, i can print the list just fine.. it's removing elements from it that becomes an problem...

4
  • 2
    How is it crashing? Are you getting an exception? Commented Mar 25, 2012 at 0:36
  • Why are you using the final keyword? Have you tried removing them? Commented Mar 25, 2012 at 0:37
  • @Jeffrey sorry, forgot to add it, added it now Commented Mar 25, 2012 at 0:38
  • @ Bernard, it doesn't make a difference if it's there or not in terms of the exception Commented Mar 25, 2012 at 0:39

3 Answers 3

13

The List returned by Arrays#asList has a fixed size. Since removeing an element would modify the length, it is unsupported. If you want a List that allows removal of items, use new ArrayList<>(Arrays.asList(array));.

Sign up to request clarification or add additional context in comments.

Comments

1

The reason why you were getting the exception has already been answered, but I add a little note about your code: You can remove all empty strings from a list in a much simpler way using the removeAll() method. You don't even have to check if the given array is empty, because this method can handle them.

String[] array = { "aa", "", "cc", "", "d" };
List<String> list = new ArrayList<String>(Arrays.asList(array));

list.removeAll(Collections.singleton(""));
System.out.println(list);

Which prints:

[aa, cc, d]

Comments

0

Arrays.asList produces a list that doesn't allow adding or removing elements.

If there is a lot elements to remove then here is a quick way to do that:

List<String> result = new ArrayList<String>();
for (String next : file) {
   if (next != null && next.length() != 0) result.add(next);
}

2 Comments

The List returned from Arrays#asList is modifiable. You are allowed to set items.
I meant you cannot remove or add elements (in context of the question). I.e. the structure is not modifiable, while elements, yes, are.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.