集册 Java实例教程 要删除特定元素,请使用remove方法。

要删除特定元素,请使用remove方法。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

547
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
它使您可以根据索引号删除元素,如下所示:emps.remove(0);
import java.util.ArrayList;


public class Main {/** nowjava 提 供 **/

  public static void main(String[] args) {

    ArrayList<Employee> emps = new ArrayList<Employee>();


    // 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);


    emps.remove(0);


    // print the array list again

    System.out.println(emps);


  }


}/**n o w j a v a . c o m - 时代Java**/

class Employee{

  private String name;


  public Employee(String name) {

    this.name = name;

  }



  public String getName() {

    return name;

  }


  public void setName(String name) {

    
展开阅读全文