集合的概念
import java.util.ArrayList;
//数组和集合都可以存放对象类型
//集合:无限制大小
//list结构集合类:ArrayList;LinkedList;Vector;Stack
//Map结构集合类:HashMap;HashTable
//Set结构集合类:HashSet;TreeSet
//Queue(队列)结构集合类:Queue接口
public class集合 {
public static void main(String[] args) {
//定义ArrayList对象(存放Object类型数据)
ArrayList al=new ArrayList();
//显示大小
System.out.println("集合大小:"+al.size());
Clerk cherk1=new Clerk("张三", 18, 1200);
al.add(cherk1);
Clerk cherk2=new Clerk("李四", 18, 1200);
al.add(cherk2);
System.out.println("集合大小:"+al.size());
//访问数据
// Clerk temp=(Clerk)al.get(0);
// System.out.println("第一个的名字是:"+temp.getName());
//遍历所有对象
for (int i = 0; i < al.size(); i++) {
Clerk temp=(Clerk)al.get(i);
System.out.println("名字是:"+temp.getName());
}
System.out.println("删除后");
//如何删除
al.remove(1);
for (int i = 0; i < al.size(); i++) {
Clerk temp=(Clerk)al.get(i);
System.out.println("名字是:"+temp.getName());
}
}
}
class Clerk{
private String name;
private int age;
private float sal;
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 getSal() {
return sal;
}
public void setSal(float sal) {
this.sal = sal;
}
public Clerk(String name, int age, float sal) {
this.name = name;
this.age = age;
this.sal = sal;
}
}