4

I want to remove an element from the ArrayList whose length is equal to the number passed as an integer. My code is as follows. When run, the programs throws UnsupportedOperationException in the line when remove() method is used. Actually, it is a codingbat problem.

public static List<String> wordsWithoutList(String[] words, int len) {    
    List<String> list = new ArrayList<String>();

    list = Arrays.asList(words);

    for(String str : list) {
        if(str.length() == len) {
            list.remove(str);
        }
    }
    return l;       
}
4
  • Ok, and what's your question? Commented Apr 23, 2012 at 7:04
  • i wanna know why it's throwing the above exception Commented Apr 23, 2012 at 7:06
  • you should cast to (List<String>) Arrays.asList(words); Commented Apr 23, 2012 at 7:07
  • you can not remove a element from a list, while you are iterating the same list Commented Apr 23, 2012 at 7:10

1 Answer 1

10

The list returned by asList isn't an ArrayList -- it doesn't support modification.

You need to do

public static List<String> wordsWithoutList(String[] words, int len) {

    List<String> l = new ArrayList<String>( Arrays.asList(words) );

    for( Iterator<String> iter = l.iterator(); iter.hasNext(); ){
        String str = iter.next();
        if(str.length()==len){
            iter.remove();
        }
    }
    return l;       
}

So two things:

  • Make a modifiable copy of the array returned by asList using the ArrayList constructor.
  • Use the iterator's remove to avoid a ConcurrentModificationException.

It was pointed out that this can be inefficient, so a nicer alternative is:

List<String> l = new ArrayList<String>(str.length());
                                   //  ^^ initial capacity optional
for( String str : words )
    if( str.length()!=len)
        l.add(str);

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

6 Comments

Curious, is this a good practice (modifying the list on which you are iterating)? or a new list should be created where we should add elements which we need and return that new ArrayList?
I think it's cleaner to create a new list and add items to it.
@zengr Either strategy works, with an ArrayList removing items from the middle moves all the following items in the list, so if there are many items to remove or the list is large, it will be better to use the copy strategy.
@trutheality your code works just fine but when i replace your for loop with the following code, it throws ConcurrentModificationException. Can you tell me why is that? Because to my understanding, the syntax of removing an object from the object is correct and the logic is also valid but it still throws exception. Thanks for(String str: l){ if(str.length()==len){ l.remove(str); } }
@zengr My point is that it's certainly not bad practice because that is exactly what the iterator's add and remove methods are for.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.