Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从ArrayList中删除元素_Java_Loops_For Loop_Arraylist - Fatal编程技术网

Java 从ArrayList中删除元素

Java 从ArrayList中删除元素,java,loops,for-loop,arraylist,Java,Loops,For Loop,Arraylist,我试图通过使用ArrayList和for循环删除字符串匹配“Meg”。到目前为止,我已经写了下面的代码,不知道为什么它不工作。我认为问题在于下面的while循环 while((customerName.get(i)).equals("Meg")) { customerName.remove(i); } 提前谢谢 完整代码如下: import java.util.ArrayList; public class CustomerLister2 { public static void ma

我试图通过使用ArrayList和for循环删除字符串匹配“Meg”。到目前为止,我已经写了下面的代码,不知道为什么它不工作。我认为问题在于下面的
while
循环

while((customerName.get(i)).equals("Meg"))
{
  customerName.remove(i);
}
提前谢谢

完整代码如下:

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);
    }
  }
}
import java.util.ArrayList;
公共类CustomerLister2
{
公共静态void main(字符串[]args)
{
ArrayList customerName=新的ArrayList();
客户名称。添加(“克里斯”);
客户名称。添加(“意向书”);
客户名称。添加(“Meg”);
客户名称。添加(“彼得”);
客户名称。添加(“炖肉”);
客户名称。添加(3,“Meg”);
客户名称。添加(4,“Brian”);
int currentSize=customerName.size();
对于(int i=0;i
将其更改为以下内容

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
  }
}
for(int i=0;i
将其更改为以下内容

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
  }
}
for(int i=0;i
或者如果不必使用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);
        }
    }
publicstaticvoidmain(字符串[]args){
ArrayList customerName=新的ArrayList();
客户名称。添加(“克里斯”);
客户名称。添加(“意向书”);
客户名称。添加(“Meg”);
客户名称。添加(“彼得”);
客户名称。添加(“炖肉”);
客户名称。添加(3,“Meg”);
客户名称。添加(4,“Brian”);
while(customerName.remove(“Meg”){}
for(字符串s:客户名称){
系统输出打印项次;
}
}

或者如果不必使用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);
        }
    }
publicstaticvoidmain(字符串[]args){
ArrayList customerName=新的ArrayList();
客户名称。添加(“克里斯”);
客户名称。添加(“意向书”);
客户名称。添加(“Meg”);
客户名称。添加(“彼得”);
客户名称。添加(“炖肉”);
客户名称。添加(3,“Meg”);
客户名称。添加(4,“Brian”);
while(customerName.remove(“Meg”){}
for(字符串s:客户名称){
系统输出打印项次;
}
}
这将对您有所帮助

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);
System.out.println(“客户名称(包括Meg)”+客户名称;
对于(inti=0;i这将帮助您

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);
System.out.println(“客户名称(包括Meg)”+客户名称;

对于(int i=0;i使用迭代器
customerName.iterator()
(迭代器()是非静态方法) 迭代器负责列表的计数修改。当我们使用
for
循环时,我们可能会忘记处理列表的计数修改

Iterator<String> itr= customerName.iterator();
while(itr.hasNext())
{
    if(itr.next().equals("Meg")){
        itr.remove();
    }
}

使用迭代器
customerName.iterator()
(迭代器()是非静态方法) 迭代器负责列表的计数修改。当我们使用
for
循环时,我们可能会忘记处理列表的计数修改

Iterator<String> itr= customerName.iterator();
while(itr.hasNext())
{
    if(itr.next().equals("Meg")){
        itr.remove();
    }
}

为什么在for-loop中使用while-loop?只有当语句(而不是while-loop)应该有workedI刚开始学习java时。谢谢你告诉我,我会记住这一点。为什么在for-loop中使用while-loop?只有if语句(而不是while-loop)应该是workedI刚开始学习java。谢谢你告诉我,我会记住这一点。如果连续元素的值为“Meg”,这将不起作用。@uba:感谢你查看这种情况。我更新了答案:)如果连续元素的值为“Meg”,这将不起作用@uba:谢谢你看这个案子。我已经更新了我的答案:)