我想从Java LinkedList Collection框架中删除用户定义的对象。下面的代码不会删除该对象。我错过了什么?
import java.util.LinkedList;
import java.util.List;
public class MyObjectKeySearch {
static class CMDTimeSlot{
String comm;
int orig;
int prio;
public CMDTimeSlot(String comm,int orig,int prio){
this.comm = comm;
this.orig = orig;
this.prio = prio;
}
}
public static void main(String a[]){
List<CMDTimeSlot> cmdlist = new LinkedList<>();
CMDTimeSlot one = new CMDTimeSlot("010D",2,2);
CMDTimeSlot two = new CMDTimeSlot("0100",1,1);
CMDTimeSlot three = new CMDTimeSlot("010C",3,3);
cmdlist.add(one);
cmdlist.add(two);
cmdlist.add(three);
for(CMDTimeSlot obj: cmdlist){
System.out.println(obj.comm +" " + obj.orig +" "+obj.prio);
}
cmdlist.remove(new CMDTimeSlot("0100",1,1) );
for(CMDTimeSlot obj: cmdlist){
System.out.println(obj.comm +" " + obj.orig +" "+obj.prio);
}
}
}
010D 2 2 0100 1 1 010C 3 3
010D 2 2
0100 1 1 010C 3 3
答案 0 :(得分:-2)
使用类equals
的成员变量覆盖hashcode
和CMDTimeSlot
方法。