Java 从数组列表中删除字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19461161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
remove a string from an array list
提问by user2872881
I keep getting this error:
我不断收到此错误:
java.lang.IndexOutOfBoundsException: Index: 3, Size: 3
java.lang.IndexOutOfBoundsException:索引:3,大小:3
I am trying to remove the string "Meg", and it will compile, but I keep getting this error.
我试图删除字符串“Meg”,它会编译,但我一直收到这个错误。
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);
}
}
}
回答by Thomas W
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()
thenset()
可能不是您想要做的。也许您只想调用set()
覆盖该索引处的现有元素,而不删除?
Remove removes an element from the list, add adds an element, set only works if the specified index exists.
Remove 从列表中删除一个元素,add 添加一个元素,set 仅在指定的索引存在时才起作用。
for ( int i = 0; i < name.size(); i++){
String oldName = name.get(i);
name.set( i, "Meg");
}
回答by Radiodef
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").
ArrayList 的大小可变。当您执行 name.remove(i) 时,列表会变小。然后您尝试将元素设置在现在不存在的索引处。您要么不需要 name.remove(i) 要么将 name.set(i, "Meg") 更改为 name.add(i, "Meg")。
http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
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.
另请注意,您的代码正在删除索引 i 处的每个元素。这可能就是您每次都在索引 3 处收到异常的原因。您删除元素 0 并且列表变小。然后删除元素 1,列表变小。依此类推,直到在 for 循环中到达 i == 3 并且列表中只有 3 个元素。
回答by mo sean
if you want to remove "Meg"
then use this
如果你想删除"Meg"
然后使用这个
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);
}
}
}
回答by Cody A. Ray
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).
我们从这个列表开始。我们将按插入顺序对 Meg 进行编号,以便于跟踪它们(已经有两个,我们还将其他名称替换为“Meg”)。
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):
我们开始i=0
并调用name.remove(0)
。所以索引 0 (Chris) 被移除,所有剩余的元素向左移动(向下移动一个索引):
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).
该调用name.set(0)
将索引 0(现在是 Lois)替换为 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
.
这结束了循环的第一遍。现在i=1
。
We remove index 1 (Meg#1) and this leaves us with:
我们删除了索引 1(Meg#1),这给我们留下了:
0: Meg#3
1: Meg#2
2: Brain
3: Peter
4: Stewie
And replace index 1 with Meg (#4):
并用 Meg (#4) 替换索引 1:
0: Meg#3
1: Meg#4
2: Brain
3: Peter
4: Stewie
Now i=2
. Remove index 2:
现在i=2
。删除索引 2:
0: Meg#3
1: Meg#4
2: Peter
3: Stewie
Replace index 2 (Peter) with Meg (#5)
用 Meg (#5) 替换索引 2 (Peter)
0: Meg#3
1: Meg#4
2: Meg#5
3: Stewie
Now i=3
. Remove index 3:
现在i=3
。删除索引 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.
现在我们尝试设置索引 3,但它不存在。所以我们得到了一个例外。
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).
这表明列表有 3 个元素(大小=3),我们尝试访问索引 3(但最大索引现在是 2)。
回答by Anurag Rana
Remove(index) will remove the item and shift rest of the items one above.
Remove(index) 将删除项目并将其余项目移至上方。
for i=0, after the loop scene is -
对于 i=0,在循环场景之后是 -
chris deleted.
0 meg (overwritten on lois)
1 meg
2 meg
3 brain
4 peter
5 stewei
克里斯已删除。
0 meg(覆盖在 lois 上)
1 meg
2 meg
3 Brain
4 peter
5stewei
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
对于 i=1 ,循环场景后为
0 meg
(meg 删除的索引移位)
1 meg(在最后一个 meg 上覆盖)
2 大脑
3 彼得
4 斯图雷
for i=2 after the loop
0 meg
1 meg
(brain deleted , indices shifted)
2 peter
3 stewei
for i=2 循环后
0 meg
1 meg
(大脑删除,索引移动)
2 peter
3steei
for i=3
when stewei is deleted
0 meg
1 meg
2 peter
for i=3 当steei被删除时
0 meg
1 meg
2 peter
no index 3 is available.... hence error is coming.
没有索引 3 可用......因此错误即将到来。