集合可以存储任意类型的对象,且长度可变
集合中不能放基本数据类型,而只能放引用数据类型
List 有序可重复
ArrayList 不适合增删,适合遍历
LinkedList
Set 接口元素无序,不重复
HashSet 良好的存取和查找
TreeSet 可以实现排序
Map 有集合映射关系
HashMap
TreeMap
package Coll;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
class Student{
String name;
int age;
Student(String name,int age){
this.name=name;
this.age=age;
}
public String toString() {
return "name "+this.name+" age "+this.age;
}
}
public class It {
public static void main(String[] args) {
//*******HashMap**********
Map map = new HashMap();
map.put("1","Jack");
map.put("2","Tom");
Set ks=map.keySet();
Iterator it = ks.iterator();
while(it.hasNext()) {
Object key = it.next();
Object value = map.get(key);
System.out.println(key+" "+value);
}
for(Object key : ks) {
Object va = map.get(key);
System.out.println(va);
}
//\*****ArrayList*******
ArrayList a = new ArrayList();
a.add("aaa");
a.add("bbb");
Iterator itl=a.iterator();
while(itl.hasNext()) {
Object obj = itl.next();
System.out.println(obj);
}
for(Object obj:a) {
System.out.println(obj);
}
//********存Student类*********
ArrayList<Student> list = new ArrayList<Student>();//泛型
list.add(new Student("aaa",12));
list.add(new Student("lily",21));
for(Student st:list) {
System.out.println(st);
}
Iterator it1 = list.iterator();
while(it1.hasNext()) {
System.out.println(it1.next());
}
}
}