集合框架知识简要-----------黑马程序员

来源:互联网 发布:第一人称冒险游戏java 编辑:程序博客网 时间:2024/06/05 17:01
集合类的由来:
对象用于封装特有数据,对象多了解需要存储,如果对象的个数不确定就使用
集合进行存储。
集合特点:
1.用于存储对象的容器
2.集合的长度是可变的
3.集合中不可以存储基本数据类型值


集合容器因为内部的数据结构不同,有多种具体容器
不断的向上抽取,就形成了集合框架。


框架的顶层 Collection接口:
1.添加。
boolean add(Object obj)
boolean addAll(Collection c)
2.删除
boolean remove(Object obj)
boolean remove(collection c)
void clear();//清空
3.判断
boolean contains(Object obj)
boolean containAll(Collection c)
boolean isEmpty();判断集合是否有元素
4.获取
int size();
Iterator iteratot();取出元素的方式,迭代器
该对象必须依赖于具体容器,因为每一个容器的 数据结构数据不同。
所以该迭代器对象是在容器内进行内部实现的。对于使用容器者而言,具体的
实现不重要,只有通过容器获取到该实现的迭代器的对象即可
也就是Iteratorfangf。

Iterator接口是对所有的collection容器进行元素取出的公共接口。
5.其他:
boolean retainAll(collection coll) 取交集
Object[] toArray;将集合转换成数组

样例1:
public class CollectionDemo {public static void main(String[] args) {Collection coll = new ArrayList();// show(coll);Collection c1 = new ArrayList();Collection c2 = new ArrayList();show(c1, c2);}public static void show(Collection co1, Collection co2) {// 给co1添加元素co1.add("abc1");co1.add("abc2");co1.add("abc3");co1.add("abc4");// 给co2添加元素co2.add("abc2");co2.add("abc5");co2.add("abc6");System.out.println("co1:" + co1);System.out.println("co2:" + co2);// addAll/* * co1.addAll(co2); System.out.println("co1:" + co1); */// removeAll// boolean b = co1.removeAll(co2);// System.out.println("removeAll:" + b);// System.out.println("co1:" + co1);// cotainsAll// boolean b = co1.containsAll(co2);// System.out.println("containsAll :" + b);// System.out.println("co1:" + co1);boolean b = co1.retainAll(co2);// 取交集,保留共同元素,删除不同元素和removeAll相反System.out.println("co1:" + co1);}public static void show(Collection coll) {// 1.添加元素coll.add("abc1");coll.add("abc2");coll.add("abc3");System.out.println(coll);// 2.删除元素.removecoll.remove("abc2");System.out.println(coll);// 3.清空集合coll.clear();System.out.println(coll);}}




-------------
collection
   |-- list: 有序(存入和取出顺序一致),元素都有索引(角标),元素可以重复
   |-- set : 元素不能重复,无序。
  
List:特有常见方法:有一个共性特点就是都可以操作角标。
1.添加
  void add(index,element);
  void add(index,collection);

2.删除 
Object remove(index)

3.修改
Object set(index,element);

4.获取
Object get(index);
int indexOf(Object);
int lastIndexOf(Object);
List subList(from,to)
list集合是可以完成对集合的增删改查


例子2:
public class ListDemo {public static void main(String[] args) {List list = new ArrayList();show(list);}public static void show(List list) {// 添加元素list.add("abc1");list.add("abc2");list.add("abc3");list.add("abc4");System.out.println(list);// 插入元素list.add(1, "abc9");System.out.println(list);// 删除元素System.out.println("remove:" + list.remove(2));System.out.println(list);// 修改元素System.out.println("set :" + list.set(1, "abc8"));System.out.println(list);// 获取元素System.out.println("get:" + list.get(0));// 获取子列表System.out.println("subList:" + list.subList(1, 2));}}



例子3:
import java.util.ArrayList;import java.util.Iterator;import java.util.List;import java.util.ListIterator;public class ListDemo2 {public static void main(String[] args) {List list = new ArrayList();show(list);System.out.println("---------------------");ListIterator iterator = list.listIterator();// 获取列表迭代器// 它可以实现在迭代过程中完成对元素的增删改查// 注意:只有list才有此类方法while (iterator.hasNext()) {Object object = iterator.next();if (object.equals("abc2")) {iterator.set("abc9");}}System.out.println("hasNext:" + iterator.hasNext());System.out.println("hasPrevious:" + iterator.hasPrevious());while (iterator.hasPrevious()) {System.out.println("previous" + iterator.previous());}System.out.println("hasNext:" + iterator.hasNext());System.out.println("hasPrevious:" + iterator.hasPrevious());System.out.println(list);/* * Iterator iterator = list.iterator(); while(iterator.hasNext()){ * Object object = iterator.next(); //在迭代器过程中,不要使用集合操作对象,容易出现异常 * //可以使用Iterator接口的子接口ListIterator来完成迭代器中队元素的操作 * if(object.equals("abc2")){ list.add("abc9"); } else * System.out.println("next:" + iterator.next()); *  * } */System.out.println("==========");System.out.println(list);}public static void show(List list) {list.add("abc1");list.add("abc2");list.add("abc3");list.add("abc4");for (Iterator iterator = list.iterator(); iterator.hasNext();) {System.out.println(iterator.next());}System.out.println("---------");// list特有的取出元素的方法for (int i = 0; i < list.size(); i++) {System.out.println("get: " + list.get(i));}}}


List:
|-- Vector:内部是数组数据结构。是同步的.100%延长,增删查询都很慢
|-- ArrayList:内部是数组数据结构,是不同步的替代了Vector,50%延长,查询速度较快
|-- LinkList:内部是连接列表实现的 ,是不同步的,增删元素的速度很快
0 0
原创粉丝点击