Java从数组列表中删除对象

Java从数组列表中删除对象,java,arraylist,Java,Arraylist,我想从对象数组列表中删除/删除对象。如果我的班级是: class bookings { public String getuser () { return user; } public Date getbooking() { return bookingDate; } private String user; private Date bookingDate; } Iterator it = list.iterator(); whi

我想从对象数组列表中删除/删除对象。如果我的班级是:

class bookings {
  public String getuser () {
    return user;
  }

  public Date getbooking() {
    return bookingDate;
  }

  private String user;
  private Date bookingDate;
}
 Iterator it = list.iterator();
        while(it.hasNext()){
            Object o = it.next();

            //check your object here such as equals, 
            //the safe way is to use Iterator here.
            it.remove();
        }
我有一个arraylist类似于:

ArrayList <bookings> book;


public void foo (String user, Date date) {
  for (bookings b : book) {
    if (user.equals(b.getuser()) && date.equals(g.getbooking()) book.remove(b);
  }
}
 Iterator it = list.iterator();
        while(it.hasNext()){
            Object o = it.next();

            //check your object here such as equals, 
            //the safe way is to use Iterator here.
            it.remove();
        }
ArrayList书;
公共void foo(字符串用户,日期){
预订(b:预订){
if(user.equals(b.getuser())和&date.equals(g.getbooking())book.remove(b);
}
}
FHr, 你的方式会有问题

java.util.ConcurrentModificationException
 Iterator it = list.iterator();
        while(it.hasNext()){
            Object o = it.next();

            //check your object here such as equals, 
            //the safe way is to use Iterator here.
            it.remove();
        }
这意味着您在循环和删除中同时修改集合

 Iterator it = list.iterator();
        while(it.hasNext()){
            Object o = it.next();

            //check your object here such as equals, 
            //the safe way is to use Iterator here.
            it.remove();
        }
如果您想知道错误发生的根本原因

 Iterator it = list.iterator();
        while(it.hasNext()){
            Object o = it.next();

            //check your object here such as equals, 
            //the safe way is to use Iterator here.
            it.remove();
        }
你最好看看这篇文章

 Iterator it = list.iterator();
        while(it.hasNext()){
            Object o = it.next();

            //check your object here such as equals, 
            //the safe way is to use Iterator here.
            it.remove();
        }

 Iterator it = list.iterator();
        while(it.hasNext()){
            Object o = it.next();

            //check your object here such as equals, 
            //the safe way is to use Iterator here.
            it.remove();
        }
所以请改用迭代器

 Iterator it = list.iterator();
        while(it.hasNext()){
            Object o = it.next();

            //check your object here such as equals, 
            //the safe way is to use Iterator here.
            it.remove();
        }
您可以使用迭代器中的bookings类型重构它,如
迭代器

请检查以下内容:

 Iterator it = list.iterator();
        while(it.hasNext()){
            Object o = it.next();

            //check your object here such as equals, 
            //the safe way is to use Iterator here.
            it.remove();
        }

请注意,Iterator.remove是在迭代过程中修改集合的唯一安全方法;如果在迭代过程中以任何其他方式修改了基础集合,则该行为未指定。

首先,您不能对(预订b:book)使用foreach循环
,因为它不允许您修改集合(它将引发异常)

 Iterator it = list.iterator();
        while(it.hasNext()){
            Object o = it.next();

            //check your object here such as equals, 
            //the safe way is to use Iterator here.
            it.remove();
        }
此代码应适用于:

 Iterator it = list.iterator();
        while(it.hasNext()){
            Object o = it.next();

            //check your object here such as equals, 
            //the safe way is to use Iterator here.
            it.remove();
        }
public void foo (String user, Date date) {
  for (Iterator<bookings> i = book.iterator(); i.hasNext();) {
    bookings b = i.next(); // Oops - forgot this line!
    if (user.equals(b.getuser()) && date.equals(b.getbooking())
        i.remove(); // Corrected this too!
  }
}
public void foo(字符串用户,日期){
for(迭代器i=book.Iterator();i.hasNext();){
bookings b=i.next();//哎呀,忘记这一行了!
if(user.equals(b.getuser())&&date.equals(b.getbooking())
i、 remove();//也更正了这个问题!
}
}
使用标准命名约定和大写字母命名也不会有什么坏处:

 Iterator it = list.iterator();
        while(it.hasNext()){
            Object o = it.next();

            //check your object here such as equals, 
            //the safe way is to use Iterator here.
            it.remove();
        }
class Booking {
    String getUser() ...
    Date getDate() ...
}

ArrayList<Booking> bookings;
订舱{
字符串getUser()。。。
日期getDate()。。。
}
ArrayList预订;

@xdazz是的,我已经尝试过了,但是当我尝试
system.out.println(b.getUser())时,列表中的对象仍然被打印出来
b仍然存在,但它不会出现在书中的arraylist中。类名总是以大写字母开头,你的基础知识越扎实,你的程序员就越好,并且永远不要用复数来表示类名。我用这种方式尝试过,当我放置两个System.out.println()时,应该是“Booking”而不是“bookings”在它的前后.remove()显示它没有被删除“将有问题”--什么问题?
if(user.equals(b.getuser())&date.equals(g.getbooking()){System.out.println(b.getuser());book.remove(b);System.out.println(b.getuser());}
@FHr您需要打印出列表,而不是b。您的目标是从书中删除b。@Clark Bao是的,我已经说过了。您声称他的代码有问题,但没有说明问题所在。
b
对象在此中未定义,我是否要将其替换为
I
?@Clark@Bohemian如何定义我?我尝试了
(预订)我
,但这是一个错误me@FHr你看到我的例子了吗?你需要尝试(booking)i.next();这里可以省略预订。@Bohemian我尝试过你的方法。使用book.remove(b)会导致java.util.ConcurrentModificationException!我不会否决。但会导致误导。@FHr让我们一起来看看