// 1) 创建一个List,在List 中增加三个工人,基本信息如下: // 姓名 年龄 工资 // zhang3 18 3000 // li4 25 3500 // wang5 22 3200 /

package com.yunhe.shangwu;

//1) 创建一个List,在List 中增加三个工人,基本信息如下:
//姓名 年龄 工资
//zhang3 18 3000
//li4 25 3500
//wang5 22 3200
//2) 在li4 之前插入一个工人,信息为:姓名:zhao6,年龄:24,工资3300
//3) 删除wang5 的信息
//4) 利用for 循环遍历,打印List 中所有工人的信息
import java.util.ArrayList;
import java.util.List;

class Worker {
	private String name;
	private int age;
	private int 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 int getSalary() {
		return salary;
	}

	public void setSalary(int salary) {
		this.salary = salary;
	}

	public Worker() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Worker(String name, int age, int salary) {
		super();
		this.name = name;
		this.age = age;
		this.salary = salary;
	}

	@Override
	public String toString() {
		return "Worker [name=" + name + ", age=" + age + ", salary=" + salary
				+ "]";
	}

}

public class GRDemo1 {
	public static void main(String[] args) {
		List list = new ArrayList();
		list.add(new Worker("zhang3", 18, 3000));
		list.add(new Worker("li4", 25, 3300));
		list.add(new Worker("wang5", 22, 3200));

		for (Worker temp : list)
			System.out.println(temp);
		System.out.println("***************");

		list.add(1, new Worker("zhao6", 20, 3100));
		for (Worker temp : list)
			System.out.println(temp);
		System.out.println("***************");

		list.remove(3);
		for (Worker temp : list)
			System.out.println(temp);
		System.out.println("***************");

	}

}

你可能感兴趣的:(作业示例代码,集合框架)