Reputation: 25
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);
}
}
}
Upvotes: 2
Views: 27613
Reputation: 1466
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.
Upvotes: 0
Reputation: 6027
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).
Upvotes: 7
Reputation: 3108
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);
}
}
}
Upvotes: 7
Reputation: 37875
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.
Upvotes: 1
Reputation: 14154
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");
}
Upvotes: 2