Collections Framework 01
-
Collection Interface
top-level interface 1.1 Collections Framework
1.2 is the parent Set List interface and
the interface is not a parent but 1.3 Map of
the collection can only add a reference to the type of data (automatic boxing and unboxing after jdk1.5) -
List Interface
List li = new ArrayList();
li.add("hh");
li.add("dd");
li.add("ww");
2.1 Features: orderly, objects can be repeated
//1)有序 数据存入顺序与输出顺序一致
for (Object it : li) {
System.out.println(it);
}
System.out.println("---------------------------");
//2)不唯一 可以出现重复数据
li.add("mmd");
for (Object it : li) {
System.out.println(it);
}
2.2 traversal
2.2.1 subscript
2.2.2 foreach (> = jdk1.5)
for (Object it : li) {
System.out.println(it);
}
2.2.3 迭代器Iterator(原理)
System.out.println("--------------iterator--------------");
Iterator it = li.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
2.3 List optimize
the initial capacity of 10,
a load factor of 0.5 (expansion ratio) 0.5
Capacity formula: container size 10 * 0.5 +
2.4 List element deletion
System.out.println("第一种");
for (int i = 0; i <li.size() ; i++) {
li.remove(0);
}
System.out.println("第二种倒叙删除");
for (int i = li.size()-1; i>=0; i--) {
li.remove(i);
}
-
Generic
After JDK1.5
to class type as a parameter called generic
role: to improve the robustness of the program, to simplify the code
default value of generic Object -
Packing, unboxing
a value type -> reference type boxed
> unboxing a value type - reference type
is introduced so automatic packing and unpacking function after jdk1.5
//1)装箱,值类型-->引用类型
int a = 4;
Integer b = new Integer(a);
//2)拆箱,应用类型-->值类型
Integer c = new Integer(3);
int d =c.intValue();
- ArrayList, LinkedList, Vector difference
ArrayList: a continuous array of structures for storing data, query block (subscript), additions and deletions slow
LinkedList: structural stored data list, query slow, additions and deletions fast
Vector: CRUD are slow, has old-fashioned
General flow chart of a set of frame