Java控制台 – 从ArrayList中删除元素,在打印时仍然显示出来

huangapple go评论70阅读模式
英文:

Java console - Remove element from arraylist, still shows up when printing

问题

我有一个ArrayList,我可以使用构造函数添加元素。这部分完全正常,当我打印时它们会显示出来,但是当我尝试删除它们时,即使在打印时仍然显示在列表中,但当我尝试第二次删除它们时,它会显示"NullPointerException",或者进入我的故障安全else if。所以问题应该出现在打印部分,或者我完全错误地思考了吗?它没有在删除元素后更新ArrayList 吗?

谢谢。

实例部分 - 创建ArrayList/构造函数(employeeVar = int)

static Employees[] employee = new Employees[employeeVar];
static List<Employees> EmployeesAL = new ArrayList<>(Arrays.asList(employee));

打印ArrayList

else {
    System.out.println("id: " + EmployeesAL.get(i).getEmployeeID() + "Name: " + EmployeesAL.get(i).getEmployeeName());
}

删除元素

else if (employee[idToRemove].getEmployeeID() == idToRemove && customer[idToRemove].getEmployeeName() != null) {
    EmployeesAL.remove(idToRemove);
    employee[idToRemove] = null;
}
}
英文:

I have an arraylist which I can add to using constructors. That works perfectly fine, and they show up when printing, but when I remove them, it still shows up in the list when printing, even though when I try to remove it for a second time, it says "NullPointerException", or goes to my failsafe else if. The problem therefore have to be in the printing part, or am I thinking completely incorrectly? Is it not updating the arraylist after removing an element?

Thank you.

Instance stuff - Creating Arraylist/constructor(employeeVar = int)

static Employees[] employee = new Employees[employeeVar];
static List&lt;Employees&gt; EmployeesAL = new ArrayList&lt;&gt;(Arrays.asList(employee));

Printing Arraylist

else{
    System.out.println(&quot;id: &quot; + EmployeesAL.get(i).getEmployeeID() + &quot;Name: &quot; + EmployeesAL.get(i).getEmployeeName());
}

Removing element

else if (employee[idToRemove].getEmployeeID() == idToRemove &amp;&amp; customer[idToRemove].getEmployeeName() != null){
    EmployeesAL.remove(idToRemove);
    employee[idToRemove] = null;
}
}

答案1

得分: 1

你设置的方式中,EmployeesALemployee 是完全未绑定的。对其中一个的更改不会反映在另一个上面。

所以,当你的 employee 中有 [Jane, Joe, Jack] 时,一开始 EmployeesAL 也是 [Jane, Joe, Jack]。但是如果你之后移除一个雇员,效果是数组中的条目被置为空,但是数组列表中的条目被移除。所以,移除 Joe 会导致 employee 变为 [Jane, null, Jack],而 EmployeesAL 变为 [Jane, Jack]。此时 Jack 在数组中的位置是索引 2(employee[2]),而在数组列表中是索引 1(EmployeesAL.get(1))。你的代码检查 employee[idToRemove],然后请求 EmployeeAL 移除该索引,很明显,这会失败。

为什么你有两种包含相同数据的数据结构?停止这样做。选择其中一个:要么使用 Employee[] employees,要么使用 List<Employee>。你可能只想要后者:

List<Employee> employees = new ArrayList<>();
employees.addAll(Arrays.asList(new Employee("Jane"), new Employee("Joe"), 等等);
英文:

The way you set it up, EmployeesAL and employee are completely unbound. A change in one is not reflected in the other.

So, when you have: [Jane, Joe, Jack] in your employee, that means at first, EmployeesAL is also [Jane, Joe, Jack]. But if you then remove an employee, the effect is that the entry in the array is nulled out, but the entry in the arraylist is removed. So, removing Joe results in employee being [Jane, null, Jack] and EmployeesAL being [Jane, Jack]. Now Jack's position in the array is index 2 (employee[2]) and in the arraylist it's 1: EmployeesAL.get(1). Your code checks employee[idToRemove] and then asks EmployeeAL to remove that index, which, obviously, is going to fail.

Why do you have 2 data structures with the same data? Stop doing that. Pick one: Either have Employee[] employees, or have List&lt;Employee&gt;. You probably want only the latter:

List&lt;Employee&gt; employees = new ArrayList&lt;&gt;();
employees.addAll(Arrays.asList(new Employee(&quot;Jane&quot;), new Employee(&quot;Joe&quot;), etc);

huangapple
  • 本文由 发表于 2020年10月2日 06:17:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/64163879.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定