package com.test.code;
import java.util.*;
public class TestList {
/**
* @param args
*/
public static void main(String[] args) {
Manage mng = new Manage();
//添加员工
Staff st1 = new Staff("刘备",45, 2000);
Staff st2 = new Staff("关羽",42, 1800);
Staff st3 = new Staff("张飞",38, 1500);
Staff st4 = new Staff("项羽",46, 3600);
mng.addStaff(st1);
mng.addStaff(st2);
mng.addStaff(st3);
mng.addStaff(st4);
mng.delStaff(); //删除员工
mng.showInfo();
}
}
/*
* 员工管理类
*/
class Manage{
private List list = null;//private ArrayList list = null;亦可,前面List为原始类型
//构造方法
public Manage(){
list = new ArrayList();
}
//加入员工
public void addStaff(Staff st){
list.add(st);
}
//显示信息
public void showInfo(){
System.out.println("姓名\t年龄\t工资");
for(int i=0; i<list.size(); i++){
Staff st = (Staff)list.get(i);
System.out.println(st.getName()+"\t"+st.getAge()+"\t"+st.getSal());
}
}
//删除员工
public void delStaff(){
list.remove(2); //删除第三位员工
}
}
/*
* 员工类
*/
class Staff{
private String name;
private int age;
private int sal;
//构造方法
public Staff(String name, int age, int sal){
this.name = name;
this.age = age;
this.sal = sal;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSal() {
return sal;
}
public void setSal(int sal) {
this.sal = sal;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
/***********************
姓名 年龄 工资
刘备 45 2000
关羽 42 1800
项羽 46 3600
***********************/
集合——ArrayList
最新推荐文章于 2022-10-04 07:35:26 发布