数据多了用对象存储。对象多了用集合存储。
集合和数组的区别:
数据用于存储统一类型的数据,有固定的长度。
集合可以存储不同类型的数据,没有固定的大小。
结合的结构 :
collection
|
|
——————————
| |
list set
------------- -----------------
| | | | |
arraylist vector linkedlist hashSet Tree Set
********************************************
collection中的常用方法
add clear reomoveAll retainALL (取交集) contains()
做实验测试类可以用实现了Collection接口的arraylist测试。
注意:在集合中存对象,存的都是对象的地址。而不是直接把对象实体存入集合中。
**************************************************
Iterator i = arrayList.iterator();
while (i.hasNext()){
system.out.println(i.next());
}
for(Iterator i =arrayList.iterator;i.hasNext();){
systemt.out.println(i.next());
};
下面的for循环相对while来说效率高些。因为i是for内部的局部变量。
********************************************************
list 和set方法的区别
list 有索引,可以存储相同的元素
set 没有索引,不能存储相同的元素
list特有的方法
增 add(index ,collection) 删 remove(index) 改 set(index,collection)
查 get(index) subList(int i,int to) 包含头不包含尾 listIterator
用迭代起操作查询集合,对集合的操作只能用迭代起的方法,而不能用集合的方法。
list迭代中特有的方法 listIterator 方法用其方法操作集合。