I have a linkedlist called seatList made up of Seat objects set up like so...
我有一个名为seatList的链表,由Seat对象组成,如此设置......
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 + ")";
}
}
When i try to remove the seats with the same properties by creating a new seat object and storing it in a seperate linked list called collections, they do not get removed from the Linked List.
当我尝试通过创建一个新的座位对象并将其存储在一个名为集合的单独链接列表中来删除具有相同属性的席位时,它们不会从链接列表中删除。
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));
}
Reason it is different: linked question talks about altering the list you are iterating as stated by csm_dev
理由是不同的:链接问题谈论改变你正在迭代的列表,如csm_dev所述
4 个解决方案
#1
0
If you really don't need to create new objects to add to collection
list, you can add objects from seatList
and second for
in you code snippet will remove objects from that list.
如果您确实不需要创建要添加到集合列表的新对象,则可以从seatList添加对象,然后在代码片段中添加对象,将从该列表中删除对象。
for (int q : indexToRemove){
collection.add(seatList.get(q));
}
When you check
weather list contains object or not, object's equals()
method is used and you should implement it accordingly along with hashCode()
method to get desired result.
当您检查天气列表是否包含对象时,使用对象的equals()方法,您应该与hashCode()方法一起实现它以获得所需的结果。
#2
1
You have to use Iterator for this:
你必须使用Iterator:
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();
}
}
You have to add setters and getters in your been class.
你必须在上课时添加setter和getter。
#3
1
To remove object from list you have to use Iterator and call remove() on iterator an example :
要从列表中删除对象,您必须使用Iterator并在迭代器上调用remove()示例:
Iterator<Seat> collection = seatList.iterator();
while (collection.hasNext())
{
Seat element = collection.next();
//some condition on your object "element"
collection.remove();
}
and don't foget to define hashCode/equals methodes logic to compare your Seat objects
并且不要忘记定义hashCode / equals方法逻辑来比较你的Seat对象
#4
0
LinkedList & other list implementations uses equals() method to check if the object is present in the list. Since you don't have equals() method implemented, java will use the implementation provided by Object class, which is very crude & not work for you.
LinkedList和其他列表实现使用equals()方法来检查对象是否存在于列表中。由于你没有实现equals()方法,java将使用Object类提供的实现,这非常粗糙,对你不起作用。
You need to implement equals() method & give jvm a way to see if 2 objects of seat are same.
你需要实现equals()方法并给jvm一种方法来查看座位的2个对象是否相同。
#1
0
If you really don't need to create new objects to add to collection
list, you can add objects from seatList
and second for
in you code snippet will remove objects from that list.
如果您确实不需要创建要添加到集合列表的新对象,则可以从seatList添加对象,然后在代码片段中添加对象,将从该列表中删除对象。
for (int q : indexToRemove){
collection.add(seatList.get(q));
}
When you check
weather list contains object or not, object's equals()
method is used and you should implement it accordingly along with hashCode()
method to get desired result.
当您检查天气列表是否包含对象时,使用对象的equals()方法,您应该与hashCode()方法一起实现它以获得所需的结果。
#2
1
You have to use Iterator for this:
你必须使用Iterator:
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();
}
}
You have to add setters and getters in your been class.
你必须在上课时添加setter和getter。
#3
1
To remove object from list you have to use Iterator and call remove() on iterator an example :
要从列表中删除对象,您必须使用Iterator并在迭代器上调用remove()示例:
Iterator<Seat> collection = seatList.iterator();
while (collection.hasNext())
{
Seat element = collection.next();
//some condition on your object "element"
collection.remove();
}
and don't foget to define hashCode/equals methodes logic to compare your Seat objects
并且不要忘记定义hashCode / equals方法逻辑来比较你的Seat对象
#4
0
LinkedList & other list implementations uses equals() method to check if the object is present in the list. Since you don't have equals() method implemented, java will use the implementation provided by Object class, which is very crude & not work for you.
LinkedList和其他列表实现使用equals()方法来检查对象是否存在于列表中。由于你没有实现equals()方法,java将使用Object类提供的实现,这非常粗糙,对你不起作用。
You need to implement equals() method & give jvm a way to see if 2 objects of seat are same.
你需要实现equals()方法并给jvm一种方法来查看座位的2个对象是否相同。