//arrayList:有序的,可重复的
List list=new ArrayList();
list.add("gansu");
list.add(19);
list.add("xian");
Collection coll=new ArrayList();
coll.add("cq");
coll.add(21);
list.addAll(coll);
System.out.println("集合大小为:"+list.size());
System.out.println("是否包含:"+list.contains("cq"));
System.out.println("indexof:"+list.indexOf("cq"));
//for循环遍历输出
System.out.println("for循环遍历输出为:");
for(int i=0;i<list.size();i++){
System.out.print(list.get(i)+"\t");
}
System.out.println();
//迭代器遍历输出
System.out.println("迭代器遍历输出为:");
Iterator iter=list.iterator();
while(iter.hasNext()){
System.out.print(iter.next()+"\t");
}
list.clear();
System.out.println("是否为空"+list.isEmpty());
Student stu1=new Student(20,"admin");
Student stu2=new Student(21,"lucy");
Student stu3=new Student(18,"jannery");
//HashMap
HashMap<Integer, Student> hm=new HashMap<Integer, Student>();
hm.put(1, stu1);
hm.put(2, stu2);
hm.put(3, stu3);
//迭代器遍历
Iterator<Integer> keyit=hm.keySet().iterator();
while(keyit.hasNext()){
Integer key=keyit.next();
Student stu=hm.get(key);
System.out.println(key+"\t"+stu.getName()+"\t"+stu.getAge());
}
//增强for循环遍历
for(Map.Entry<Integer, Student> me:hm. entrySet()){
Student stu=me.getValue();
System.out.println(me.getKey()+"\t"+stu.getAge()+"\t"+stu.getName());
}
//values遍历值
for(Student stu:hm.values()){
System.out.println(stu.getName()+"\t"+stu.getAge());
}
三种遍历方式
最新推荐文章于 2023-04-15 19:30:26 发布