This site requires JavaScript, please enable it in your browser!
Greenfoot back
JasonZhu
JasonZhu wrote ...

2014/3/18

For each loop and removal in ArrayList

JasonZhu JasonZhu

2014/3/18

#
1
2
3
4
5
6
7
8
9
10
11
12
13
/**
 * Post: all words that are exactly len letters long have been
 * removed from this WordList, with the order of the remaining words
 * unchanged.
 */
public void removeWordsOfLength(int len)
{
    for(String s:myList){
        if(s.length()==len){
            myList.remove(s);
        }
    }
}
This errors out during compilation. Is it because ArrayLists auto order itself after each removal? As in the Strings under the item removed are automatically shifted upwards to replace the empty cell?
danpost danpost

2014/3/18

#
You are probably getting the 'concurrentModificationException'. This indicates that you are trying to change the list while using the list. Try this:
1
2
3
4
5
public void removeWordsOfLength(int len)
{
    List<String> dup = new ArrayList(myList);
    for (String s: dup) if (s.length() == len) myList.remove(s);
}
danpost danpost

2014/3/18

#
This might work also:
1
2
3
4
public void removeWordsOfLength(int len)
{
    for (int i=0; i<myList.size() && myList.size() > 0; i++) if (myList.get(i).length() == len) myList.remove(i--);
}
JasonZhu JasonZhu

2014/3/18

#
I tried your second method and it works. I don't understand why it has to be i-- though. I tried it with on i, but when there are 2 words one under another that are of equal removal length, it only removes the top one.
danpost danpost

2014/3/18

#
JasonZhu wrote...
I tried your second method and it works. I don't understand why it has to be i-- though. I tried it with on i, but when there are 2 words one under another that are of equal removal length, it only removes the top one.
When you remove an item from the list, the words are shifted toward the top of the list. If you do not shift 'i', you will miss what has been shifted into the place you just removed an item from.
JasonZhu JasonZhu

2014/3/18

#
Ok, I see the functionality of the removal now. It removes i and sets it back 1. Thank you danpost!
danpost danpost

2014/3/18

#
JasonZhu wrote...
Ok, I see the functionality of the removal now. It removes i and sets it back 1. Thank you danpost!
I do not know if 'sets it back 1' would be the right terminology. I would say it decreases the loop counter (or index in the list) so that the next iteration of the 'for' loop, which then increases it back to the same number it was, can work on the next item in the list, which is now at the same location the removed item was at.
JasonZhu JasonZhu

2014/3/18

#
That is what I meant :).
davmac davmac

2014/3/19

#
Just for information, there is a slightly more efficient way of doing this:
1
2
3
4
5
6
7
8
9
public void removeWordsOfLength(int len) 
    Iterator<String> i = myList.iterator();
    while(i.hasNext()){ 
        if(i.next().length()==len){ 
            i.remove(); 
        
    
JasonZhu JasonZhu

2014/3/19

#
Good to know, thank you for sharing davmac!
You need to login to post a reply.