ByRef修改List

import java.util.ArrayList;

public class UpdateObjectsInListByRef {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    ArrayList emps = new ArrayList();

    // add employees to array list
    emps.add(new Employee("A"));
    emps.add(new Employee("T"));
    emps.add(new Employee("K"));

    // print array list
    System.out.println(emps);

    // change one of the employee's names
    Employee e = emps.get(1);
    e.setName("new name");

    // print the array list again
    System.out.println(emps);
}

}
class Employee{
private String name;

  public Employee(String name) {
    this.name = name;
  }


  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  @Override
  public String toString() {
    return "Employee [name=" + name + "]";
  }

}

Console:
[Employee [name=A], Employee [name=T], Employee [name=K]]
[Employee [name=A], Employee [name=new name], Employee [name=K]]

你可能感兴趣的:(ByRef修改List)