如何删除 Linkedlist 中的对象。我有一个带有 studentId 和 studentName 的班级帐户。我在列表中输入了对象,但是当我尝试删除时,我不知道该怎么做。因为每次从列表中间删除一个元素时,它都会被组织起来,这意味着索引会发生变化。那么如何获取 studentId 属性并删除linkedList中的对象。
样本:
LinkedList: Account{studentId = 1, studentName = nome1} = index = 0 ,
LinkedList: Account{studentId = 2, studentName = nome2} = index = 1 ,
LinkedList: Account{studentId = 3, studentName = nome3} = index = 2.
我想要的是用户插入他想要删除的 studentId,我可以做一个搜索和删除该对象的代码。
public Account{
private int studentID;
private String StudentName;
}
public static void main(String[] args){
int accountNumber;
LinkedList<Account> linkedAccount = new LinkedList<>();
Account obj1;
System.out.println("Type the acc number: ");
accountNumber = in.nextInt();
obj1 = linkedAccount.remove(accountNumber);
System.out.println("The " + obj1 + " has been deleted");
}
每次我从中间删除一个对象时,它都会更改linkedList 的索引。重新排列。所以我不知道该怎么做你能帮我吗?