Vector ArrayList
Vector vlist = new Vector();
vlist.add(1);
vlist.add("abc");
vlist.addElement("456");
vlist.add(3,"eee");
vlist.add(4,"eee");
vlist.add(5,"eee");
vlist.add(6,"eee");
vlist.add(7,"eee");
vlist.add(8,"eee");
vlist.add(9,"eee9");
vlist.add(10,"ee10");
System.out.println(" vlist capacity" + vlist.capacity());
int r = vlist.indexOf("eee");
System.out.println(" vlist r indexof" + r);
Enumeration vEnum = vlist.elements();
while(vEnum.hasMoreElements()) {
System.out.println("vlist="+vEnum.nextElement());
}
ArrayList arlist = new ArrayList();
arlist.add(1);
arlist.add("abc");
arlist.add("456");
arlist.add(3,"eee");
arlist.add(4,"eee");
arlist.add(5,"eee");
arlist.add(6,"eee");
arlist.add(7,"eee");
arlist.add(8,"eee");
arlist.add(9,"eee9arlist");
arlist.add(10,"ee10");
System.out.println(" arlist size" + arlist.size());
for (int i = 0; i < arlist.size(); i++) {
System.out.println("arlist="+arlist.get(i));
}
}
vlist capacity20
vlist r3
vlist=1
vlist=abc
vlist=456
vlist=eee
vlist=eee
vlist=eee
vlist=eee
vlist=eee
vlist=eee
vlist=eee9
vlist=ee10
arlist size11
arlist=1
arlist=abc
arlist=456
arlist=eee
arlist=eee
arlist=eee
arlist=eee
arlist=eee
arlist=eee
arlist=eee9arlist
arlist=ee10
区别
- ArrayList在内存不够时默认是扩展50% + 1个,Vector是默认扩展1倍。
- Vector提供indexOf(obj, start)接口,ArrayList没有。