/**
* ArrayList集合讲解
* 集合的添加、遍历、删除
*/
package com.test;
import java.util.*;
public class test7 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//添加第一个职员
Clerk clerk1 = new Clerk();
clerk1.setName("kim");
clerk1.setAge(18);
clerk1.setSalary(1000.00f);
//添加第二个职员
Clerk clerk2 = new Clerk();
clerk2.setName("ross");
clerk2.setAge(19);
clerk2.setSalary(2000.00f);
ArrayList al = new ArrayList();
al.add(clerk1);
al.add(clerk2);
//打印各个对象的信息
Clerk temp = (Clerk)al.get(0);
Clerk temp_1 = (Clerk)al.get(1);
System.out.println("第1个人的信息:"+"姓名:"+temp.getName()+" 年龄:"+temp.getAge()+" 工资:"+temp.getSalary());
System.out.println("第2个人的信息:"+"姓名:"+temp_1.getName()+" 年龄:"+temp_1.getAge()+" 工资:"+temp_1.getSalary());
//for循环打印对象信息
for(int i=0; i<al.size(); i++)
{
Clerk temp_2 = (Clerk)al.get(i);
System.out.println("第"+(i+1)+"个人的信息:"+"姓名:"+temp_2.getName()+" 年龄:"+temp_2.getAge()+" 工资:"+temp_2.getSalary());
}
al.remove(0); //删除ArrayList中第一个元素
System.out.println("---------删除集合元素后----------------");
//for循环打印对象信息
for(int i=0; i<al.size(); i++)
{
Clerk temp_2 = (Clerk)al.get(i);
System.out.println("第"+(i+1)+"个人的信息:"+"姓名:"+temp_2.getName()+" 年龄:"+temp_2.getAge()+" 工资:"+temp_2.getSalary());
}
}
}
class Clerk
{
private String name;
private int age;
private float 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 float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}
}
小结: