我有一個問題,我的代碼,我做了一個示例程序來顯示鏈接列表中的emp詳細信息,現在問題,當我試圖刪除一個特定的條目意味着它不工作,我希望我做了一些錯誤我的代碼可以建議如何做到這一點?如何從java中的鏈表中刪除一個對象?
import java.util.*;
class EmpDedup {
int record;
String fprint;
int fid;
EmpDedup(int record, String fprint, int fid) {
this.record = record;
this.fprint = fprint;
this.fid = fid;
}
public int getRecord() {
return record;
}
public String getFprint() {
return fprint;
}
public int getFid() {
return fid;
}
public static void main(String[] args) {
int count = 0;
LinkedList<EmpDedup> list = new LinkedList<EmpDedup>();
list.add(new EmpDedup(101, "entry1", 20));
list.add(new EmpDedup(102, "entry2", 30));
list.add(new EmpDedup(103, "entry3", 40));
list.add(new EmpDedup(104, "entry4", 50));
Scanner input = new Scanner(System.in);
System.out.print("Enter record no to display: ");
int rec = input.nextInt();
for (EmpDedup data : list) {
if (data.getRecord() == rec) {
System.out.println(data.getRecord() + "\t" + data.getFprint() + "\t" + data.getFid() + "\t");
count++;
}
}
System.out.println("The size of an linkedlist is: \t" + list.size());
System.out.println("The number of available record is :" + count);
System.out.println("The size of an linkedlist is: \t" + list.size());
Scanner input1 = new Scanner(System.in);
System.out.print("Enter record no to delete: ");// here i try to delete a particular record
int rec1 = input1.nextInt();
for (EmpDedup data : list) {
if (data.getRecord() == rec1) {
// System.out.println(data.getRecord()+"\t"+data.getFprint()+"\t"+data.getFid()+"\t");
list.remove(data); // problem is here
count++;
}
}
}
}
「無法工作」 的頭 - 請更多的描述。發生了什麼? –
list.remove(101);如果這不是list.remove(data); –