2

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);
        }
    }
}
1
  • What is the purpose of name.set(i,"Meg");? Commented Oct 19, 2013 at 1:10

5 Answers 5

7

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);
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

7

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).

1 Comment

I thought the end list was "Lois, Meg, Peter". I forgot about the adds. Bravo.
2

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");
}

Comments

1

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.

3 Comments

Index 3 will still exist in this case I believe.
@JeroenVannevel After each iteration, the size of the list has been reduced by 1, on the fourth iteration (when i is 3), the list has a size of 3 when set(3) is called.
When the exception gets thrown, the OP's list looks like: "Lois", "Meg", "Peter".
0

Remove(index) will remove the item and shift rest of the items one above.

for i=0, after the loop scene is -

chris deleted.
0 meg (overwritten on lois)
1 meg
2 meg
3 brain
4 peter
5 stewei

for i=1 , after loop scene is
0 meg
(meg deleted indices shifted)
1 meg (overwritten on last meg)
2 brain
3 peter
4 stewei

for i=2 after the loop
0 meg
1 meg
(brain deleted , indices shifted)
2 peter
3 stewei

for i=3 when stewei is deleted
0 meg
1 meg
2 peter

no index 3 is available.... hence error is coming.

Comments

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.