Java 中数组声明后不可修改 –跟C一样 ,C中往往会增加定义HASHTABLE,链表等结构来实现动态的数组
但在java中,已经有现成的,称为集合
Collection (interface,有迭代器)
├List (interface)
│├LinkedList (class)
│├ArrayList (class)
│└Vector (class,同步)
│ └Stack (class,栈)
├Set (interface,不重复元素)
│├SortedSet (interface)
│├AbstractSet (abstract class)
││├HashSet(class 类似set,基于hashmap)
││├TreeSet (class类似set, 带自动排序,基于Treemap)
Map(interface,有迭代器,key,value)
├Hashtable
├HashMap (同步)
|- TreeMap (带自动排序)
└WeakHashMap (自动GC的hashmap)
Map.Entry Map 的每个子元素接口,所有map均可使用
所有集合均实现迭代器
//迭代器循环
Hashtable<String, String> ffh= new Hashtable<String,String>();
ffh.put("a", "b");
ffh.put("u", "fff");
Iterator<String> ddf=ffh.keySet().iterator();
while (ddf.hasNext()) {
String k=ddf.next();
System.out.print("Key"+k);
String v=ffh.get(k);
System.out.print("Vlau"+v);
}
//for
Vector<String> ddfe= new Vector<String>();
ddfe.add("fasdfa");
ddfe.add("");
for(String fefe:ddfe){
System.out.print(fefe);
}
//for
HashMap<String, String> hm= new HashMap<String,String>();
hm.put("ddd","ccc");
for(Map.Entry<String, String> temp:hm.entrySet()){
System.out.print(temp.getKey());
System.out.print(temp.getValue());
}
//for
TreeMap<String,String> ddff=new TreeMap<String,String>();
ddff.put("c", "d");
ddff.put("a", "b");
for(Map.Entry<String, String> temp:ddff.entrySet()){
System.out.print(temp.getKey());
System.out.print(temp.getValue());
}
//for
Vector<String> tset= new Vector<String>();
tset.add("a");
tset.add("b");
for(String temp:tset){
System.out.print(temp);
}
//将数组转为集合,不能修改元素
String [] c={"a","b"};
List<String> temp=Arrays.asList(c);