[英]Removing an element from a ArrayList
I'm trying to remove the String match "Meg" by using the ArrayList and for loop. 我试图通过使用ArrayList和for循环来删除字符串匹配“ Meg”。 So far I have written the code below and not sure why it's not working.
到目前为止,我已经在下面编写了代码,不确定为什么它不起作用。 I think the problem is with the below
while
loop 我认为问题在于下面的
while
循环
while((customerName.get(i)).equals("Meg"))
{
customerName.remove(i);
}
Thanks in advance. 提前致谢。
Complete code is as below: 完整的代码如下:
import java.util.ArrayList;
public class CustomerLister2
{
public static void main(String[] args)
{
ArrayList<String> customerName = new ArrayList<String>();
customerName.add("Chris");
customerName.add("Lois");
customerName.add("Meg");
customerName.add("Peter");
customerName.add("Stewie");
customerName.add(3, "Meg");
customerName.add(4, "Brian");
int currentSize = customerName.size();
for(int i = 0; i < currentSize - 1; i++)
{
while((customerName.get(i)).equals("Meg"))
{
customerName.remove(i);
}
}
for(String newStr: customerName)
{
System.out.println(newStr);
}
}
}
Change it to the following 将其更改为以下内容
for(int i = 0; i < currentSize; i++)
{
if((customerName.get(i)).equals("Meg"))
{
customerName.remove(i);
i--; //because a new element may be at i now
currentSize = customerName.size(); //size has changed
}
}
Or if you don't have to use a for loop: 或者,如果您不必使用for循环:
public static void main(String[] args) {
ArrayList<String> customerName = new ArrayList<String>();
customerName.add("Chris");
customerName.add("Lois");
customerName.add("Meg");
customerName.add("Peter");
customerName.add("Stewie");
customerName.add(3, "Meg");
customerName.add(4, "Brian");
while (customerName.remove("Meg")) {}
for (String s : customerName) {
System.out.println(s);
}
}
This will help you 这对你有帮助
System.out.println("Customer Name (including Meg)"+customerName);
for(int i=0; i<customerName.size(); i++)
{
String s = customerName.get(i);
if(s.equals("Meg"))
{
customerName.remove(i);
i--;
}
}
System.out.println("Customer Name (excluding Meg)"+customerName);
Use iterator customerName.iterator()
(iterator() is non-static method) Iterator takes care of list's count modification. 使用迭代器
customerName.iterator()
(iterator()是非静态方法)迭代器负责列表计数的修改。 We might forget to take care of list's count modification when we use for
loop. 当使用
for
循环时,我们可能会忘记照顾列表的计数修改。
Iterator<String> itr= customerName.iterator();
while(itr.hasNext())
{
if(itr.next().equals("Meg")){
itr.remove();
}
}
Drawback also there in iterator. 迭代器中也有缺点。 It doesn't work if two threads are concurrently accessing the same list object.
如果两个线程同时访问同一个列表对象,则无法正常工作。 Ex.
例如 One thread is reading and another thread is removing element from the list then it throws
java.util.ConcurrentModificationException.
一个线程正在读取,另一个线程正在从列表中删除元素,然后引发
java.util.ConcurrentModificationException.
Better to use Vector
in concurrent scenario. 最好在并发方案中使用
Vector
。
If you still want to use same for
loop then add following line of code. 如果您仍想使用相同的
for
循环,请添加以下代码行。
int currentSize = customerName.size();
for(int i = 0; i < currentSize; i++)
{
while((customerName.get(i)).equals("Meg"))
{
customerName.remove(i);
currentSize = customerName.size(); //add this line to avoid run-time exception.
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.