基础练习题

package a.aaa;

//import java.util.ArrayList;
import java.util.*;
/*
 * 
 * 1.已知有一个工人Worker类 属性:姓名 年龄 工资, 行为 void work()
    a.在ArrayList集合中添加三个工人对象,信息如下:
        zhang3  18   3000
        li4      25  3500
        wang5    22  3200
    b. 在li4之前插入一个工人 信息为: zhao6  24  3300
    c. 删除wang5的信息
    d. 利用for循环遍历,打印几个中所有工人的信息
    e. 利用迭代遍历,对集合中的所有工人调用work方法
————————————————
版权声明:本文为CSDN博主「M新」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_35292921/article/details/78534211
 * */
public class List1 {
  public static void main(String[] args) {
	  ArrayList <Worker> arr=new ArrayList();
	  Worker worker1=new Worker("zhang3", 18 , 3000);
	  Worker worker2=new Worker("li4",  25 , 3500);
	  Worker worker3=new Worker("wang5", 22 , 3200);
	  arr.add(worker1);
	  arr.add(worker2);
	  arr.add(worker3);
	  for(Worker worker:arr) {
		  System.out.println(worker);
	  }
	  Worker worker4=new Worker( "zhao6"  ,24  ,3300);
	  arr.add(1,worker4);
	  System.out.println("////////");
	  for(Worker worker:arr) {
		  System.out.println(worker);
	  }
	  arr.remove(3);
	  System.out.println("删除wang5////////");
	  for(Worker worker:arr) {
		  System.out.println(worker);
	  }
	  System.out.println("Iterator遍历////////");
	  Iterator<Worker> it=arr.iterator();
	  while(it.hasNext()) {
		  Worker worker=it.next();
		  worker.work();
	  }
	  
 }
}

package a.aaa;

public class Worker {
	private String name;
	private int age;
	private double salary;
	public Worker(String string, int i, int j) {
		// TODO Auto-generated constructor stub
		name=string;
		age=i;
		salary=j;
	}
	public void work() {
		System.out.println("调用"+name+"的work方法");
	}
	@Override
	public String toString() {
		return "Worker [name=" + name + ", age=" + age + ", salary=" + salary + "]";
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public double getSalary() {
		return salary;
	}
	public void setSalary(double salary) {
		this.salary = salary;
	}

}

posted @ 2021-05-07 13:26  渔_夫  阅读(41)  评论(0编辑  收藏  举报