通过引用删除对象

import java.util.ArrayList;

public class ByRefRemove {

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

    // add Employee2s to array list
    emps.add(new Employee2("A"));
    emps.add(new Employee2("T"));
    emps.add(new Employee2("K"));

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

    emps.remove(0);

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

}
class Employee2{
private String name;

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


  public String getName() {
    return name;
  }

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


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

}

Console:
[Employee2 [name=A], Employee2 [name=T], Employee2 [name=K]]
[Employee2 [name=T], Employee2 [name=K]]

你可能感兴趣的:(通过引用删除对象)