[java]代码库
import java.util.ArrayList;
import java.util.HashMap;
/**
* @ClassName: ArrayListTest2
* @Description: TODO(这里用一句话描述这个类的作用)
* @author Zhouwei
* @date 2014-4-3 上午9:03:21
*
*/
public class ArrayListTest2 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Student zhoutao=new Student("周涛",19);
Student chenwenjie=new Student("陈文杰",20);
Student qiaoxin=new Student("乔鑫",20);
ArrayList arrList=new ArrayList();
arrList.add(zhoutao);
arrList.add(chenwenjie);
arrList.add(qiaoxin);
//查询name属性="陈文杰 " 的student 对象
for(int i=0;i<arrList.size();i++){
//取出元素,需要强转
Student s=(Student)arrList.get(i);
if(s.getName().equals("陈文杰")){
System.out.println(s);
}
}
//泛型集合 ArraList<元素类型>
ArrayList<Student> list=new ArrayList<Student>();
list.add(zhoutao);
list.add(chenwenjie);
list.add(qiaoxin);
//泛型集合的好处,取元素不需强转
Student s=list.get(0);
HashMap<String, Student> map=new HashMap<String, Student>();
}
}