创建一个list,在list中增加三个工人,基本信息如下: 姓名 年龄 工资 zhang3 18 3000 li4 25 3500 wang5 22 3200 在li4之前插入一个工人,信息为:姓名:zhao6,年龄:24,工资3300 删除wang5的信息 package list02; public class Worker { String wname; int age; double wage; public Worker(String wname, int age,double wage){ this.wname = wname; this.age = age; this.wage=wage; } public Worker(){} public String getWname() { return wname; } public void setWname(String wname){ this.wname = wname; } public int getAge() { return age; } public void setAge(int age){ this.age = age; } public double getWage() { return wage; } public void setWage(double wage) { this.wage = wage; } @Override public String toString() { return "Worker[名字"+wname+",年龄"+age+",工资"+wage+"]"; } }
package list02; import java.util.ArrayList; import java.util.List; public class Test { public static void main(String[] args) { List<Worker>list=new ArrayList<>(); list.add(new Worker("zhang3",18,3000)); list.add(new Worker("li4",25,3500)); list.add(new Worker("wang5",22,3200)); for (Worker w : list){ System.out.println(w); }System.out.println("*********"); list.add(1,new Worker("zhao6",24,300)); for (Worker w : list){ System.out.println(w); }System.out.println("*********"); list.remove(3); for (Worker w : list){ System.out.println(w); } } }