remove a string from an array list

26,750

Solution 1

if you want to remove "Meg" then use this

import java.util.ArrayList;

public class CustomerLister2 {
    public static void main (String[] args) {

        ArrayList<String> name = new ArrayList<String>();

        name.add("Chris");
        name.add("Lois");
        name.add("Meg");
        name.add("Meg");
        name.add("Brain");
        name.add("Peter");
        name.add("Stewie");


        for ( int i = 0;  i < name.size(); i++){
            String tempName = name.get(i);
            if(tempName.equals("Meg"){
                name.remove(i);
            }
        }

        for(String names: name){
            System.out.println(names);
        }
    }
}

Solution 2

Let's trace through this algorithm.

We start with this list. We'll number the Megs in insertion order to make them easier to track (there's already two and we're replacing other names with "Meg" as well).

0: Chris
1: Lois
2: Meg#1
3: Meg#2
4: Brain
5: Peter
6: Stewie

We start with i=0 and call name.remove(0). So index 0 (Chris) gets removed and all the remaining elements shift left (down an index):

0: Lois
1: Meg#1
2: Meg#2
3: Brain
4: Peter
5: Stewie

Notice that the list is now one element smaller.

The call name.set(0) replaces index 0 (now Lois) with Meg (#3).

0: Meg#3
1: Meg#1
2: Meg#2
3: Brain
4: Peter
5: Stewie

This concludes the first pass of the loop. Now i=1.

We remove index 1 (Meg#1) and this leaves us with:

0: Meg#3
1: Meg#2
2: Brain
3: Peter
4: Stewie

And replace index 1 with Meg (#4):

0: Meg#3
1: Meg#4
2: Brain
3: Peter
4: Stewie

Now i=2. Remove index 2:

0: Meg#3
1: Meg#4
2: Peter
3: Stewie

Replace index 2 (Peter) with Meg (#5)

0: Meg#3
1: Meg#4
2: Meg#5
3: Stewie

Now i=3. Remove index 3:

0: Meg#3
1: Meg#4
2: Peter

Now we try to set index 3, but it doesn't exist. So we get an exception.

java.lang.IndexOutOfBoundsException: Index: 3, Size: 3

This shows that the list has 3 elements (size=3) and we tried to access index 3 (but the max index is now 2).

Solution 3

Calling remove() then set() is probably not what you want to do. Maybe you just want to call set() to overwrite the existing element at that index, without removing?

Remove removes an element from the list, add adds an element, set only works if the specified index exists.

for ( int i = 0;  i < name.size(); i++){
    String oldName = name.get(i);
    name.set( i, "Meg");
}

Solution 4

ArrayLists are of a variable size. When you do name.remove(i), the list gets smaller. Then you try to set the element at a now nonexistent index. You either need to not name.remove(i) or change name.set(i, "Meg") to name.add(i, "Meg").

http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html

Edit

Also note that your code is removing every element at index i. That's probably why you are getting the exception at index 3 every time. You remove element 0 and the list gets smaller. Then you remove element 1 and the list gets smaller. And so on until you get to i == 3 in your for loop and the list has only 3 elements in it.

Share:
26,750
user2872881
Author by

user2872881

Updated on October 19, 2020

Comments

  • user2872881
    user2872881 over 3 years

    I keep getting this error:

    java.lang.IndexOutOfBoundsException: Index: 3, Size: 3

    I am trying to remove the string "Meg", and it will compile, but I keep getting this error.

    import java.util.ArrayList;
    
    public class CustomerLister2 {
        public static void main (String[] args) {
    
            ArrayList<String> name = new ArrayList<String>();
    
            name.add("Chris");
            name.add("Lois");
            name.add("Meg");
            name.add("Meg");
            name.add("Brain");
            name.add("Peter");
            name.add("Stewie");
    
            for ( int i = 0;  i < name.size(); i++){
                name.get(i);
                name.remove(i);
                name.set(i,"Meg");
            }
    
            for(String names: name){
                System.out.println(names);
            }
        }
    }