请参考下面的代码,让我知道我哪里出错了
public class EmployeeTest {
public static void main(String[] args) {
ArrayList<Employee> list = new ArrayList<>();
list.add(new Employee("1026751","Jack Reacher"));
list.add(new Employee("1026722","Tom Cruise"));
System.out.println(list);
//Employee a = list.get(0);
Employee e = list.get(0);
Employee p = list.get(1);
list.clear();
e.setEmployeeName("Rishabh Bansal");
list.add(e);
e.setEmployeeName("Jack Reacher");
list.add(e);
list.add(p);
System.out.println(list);
}
}
我想在上面添加元素
0 index as "1026751","Rishabh Bansal"
1 index as "1026751","Jack Reacher"
2 index as "1026722","Tom Cruise"
但不是它,我得到的列表是-:
[Employee [employeeId=1026751, employeeName=Jack Reacher], Employee [employeeId=1026751, employeeName=Jack Reacher], Employee [employeeId=1026722, employeeName=Tom Cruise]]
为什么 0 索引上的元素正在改变......
慕少森
杨魅力
相关分类