Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.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中从链表中删除对象_Java_Object - Fatal编程技术网

如何在java中从链表中删除对象

如何在java中从链表中删除对象,java,object,Java,Object,我有一个名为seatList的linkedlist,由座椅对象组成,设置如下 public class Seat { int row, col; char type; Seat(int row, int col, char type){ this.row = row; this.col = col; this.type = type; } String printSeat(){ retu

我有一个名为seatList的linkedlist,由座椅对象组成,设置如下

public class Seat {

    int row, col;
    char type;

    Seat(int row, int col, char type){
        this.row = row;
        this.col = col;
        this.type = type;
    }

    String printSeat(){
        return "(" + row + ", " + col + ", " + type + ")";
    }

}
当我试图通过创建一个新的座椅对象并将其存储在称为“集合”的单独链接列表中来删除具有相同属性的座椅时,它们不会从链接列表中删除

LinkedList<Seat> collection = new LinkedList();

    for (int q : indexToRemove){
        collection.add(new Seat(seatList.get(q).row, seatList.get(q).col, seatList.get(q).type));
    }

    for (Seat s : collection){
        if (seatList.contains(s)){
            println("true");
            seatList.remove(s);
        }

    }

    if (orders.get(indexes.get(index)).seatList.isEmpty()){
        orders.remove((int)indexes.get(index));
    }
LinkedList集合=新建LinkedList();
for(int q:indexToRemove){
添加(新座位(seatList.get(q).row,seatList.get(q).col,seatList.get(q).type));
}
用于(座椅s:收集){
如果(座位列表包含){
println(“真”);
座椅列表。移除(s);
}
}
if(orders.get(index.get(index)).seatList.isEmpty()){
remove((int)index.get(index));
}

不同的原因:链接问题涉及更改csm_dev所述的正在迭代的列表

您必须为此使用迭代器:

Scanner scr= new Scanner(System.in);
System.out.print("Enter Seat Row number to delete: ");
int row = scr.nextInt();

for (Iterator<Seat> iter = list.iterator(); iter.hasNext();) {

    Seat data = iter.next();

    if (data.getRow() == row) {
        iter.remove();
    }
}
Scanner scr=新扫描仪(System.in);
系统输出打印(“输入要删除的座椅排号:”);
int row=scr.nextInt();
for(迭代器iter=list.Iterator();iter.hasNext();){
座椅数据=iter.next();
if(data.getRow()==行){
iter.remove();
}
}

必须在BER类中添加setter和getter。

LinkedList&其他列表实现使用equals()方法检查对象是否存在于列表中。由于没有实现equals()方法,java将使用Object类提供的实现,这非常粗糙&不适合您


您需要实现equals()方法,并为jvm提供一种查看seat的两个对象是否相同的方法。

要从列表中删除对象,您必须使用迭代器并在迭代器上调用remove(),例如:

    Iterator<Seat> collection = seatList.iterator();
    while (collection.hasNext()) 
    {
       Seat element = collection.next();

       //some condition on your object "element"

        collection.remove();
      }
Iterator collection=seatList.Iterator();
while(collection.hasNext())
{
Seat元素=collection.next();
//对象“元素”的某些条件
collection.remove();
}

不要忘记定义hashCode/equals方法逻辑来比较座位对象

如果您真的不需要创建新对象来添加到
集合
列表中,您可以从
座位列表
中添加对象,代码片段中的第二个
for
将从该列表中删除对象

for (int q : indexToRemove){
    collection.add(seatList.get(q));
}

当您
检查天气列表是否包含对象时,将使用对象的
equals()
方法,您应该将其与
hashCode()一起相应地实现
方法以获得所需的结果。

您需要在Seat Objects中实现hashCode/equals/compareTo方法上述链接问题的可能重复内容涉及更改您正在迭代的列表,但此处并非如此。此解决方案仍然不可行。在seat类中不需要getter和setter对于我的解决方案,它要求在删除元素后不重新调整linkedlist,因此在从linkedlist中删除一个元素后,所有其他元素上的所有索引都会更改?这会一直起作用,直到您删除多个元素时,它会删除具有过去索引的元素。有什么办法吗?是的,你是对的。您应该重写equals/hashCode,或者另一个不太优雅的解决方案是用添加到“collection”的新创建的对象替换“seatList”中的旧对象。之后,您的包含检查将工作。添加到集合时是否确实需要创建新对象?如果没有,您可以将对象从“seatList”添加到“collection”:collection.add(seatList.get(q));之后,第二个
for
将从原始列表中删除它们。刚刚尝试了最后一部分,效果很好谢谢!