如何从arraylist中显示删除节点?

时间:2013-09-13 06:52:42

标签: java arraylist collections

public static void deleteEmployee(int empId) {
        Iterator<Employee> iterator = list1.iterator();
        while (iterator.hasNext()) {
            if (iterator.next().getEmpid() == empId) {
                System.out.println("The name of whose details deleted is  ::");
                iterator.remove();
            }
        }

    }

这里我试图根据员工ID从arraylist中删除节点。但在删除之前我想显示特定员工的详细信息。我怎么能这样做?

2 个答案:

答案 0 :(得分:4)

如何将员工存储在临时变量中?

public static void deleteEmployee(int empId) {
    Iterator<Employee> iterator = list1.iterator();
    while (iterator.hasNext()) {
        Employee employee = iterator.next();
        if (employee.getEmpid() == empId) {
            System.out.println("The name of whose details deleted is  ::" + employee.getName()); // or whatever property in employee
            iterator.remove();
        }
    }

}

答案 1 :(得分:0)

这与IteratorList无关。

查看Employee:是否有toString()实施打印详细信息?如果没有,它的属性是否有吸气剂?与System.out.println一起使用。