黑马程序员——JAVA集合

----<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! ------

集合(Collection)
(1)集合的由来?
我们学习的是Java -- 面向对象 -- 操作很多对象 -- 存储 -- 容器(数组和StringBuffer) -- 数组
而数组的长度固定,所以不适合做变化的需求,Java就提供了集合供我们使用。
(2)集合和数组的区别?
A:长度区别
数组固定
集合可变
B:内容区别
数组可以是基本类型,也可以是引用类型
集合只能是引用类型
C:元素内容
数组只能存储同一种类型
集合可以存储不同类型(其实集合一般存储的也是同一种类型)

集合框架的构成及分类:

Collection接口
框架的顶层Collection接口:
 Collection的常见方法:
     1、添加:
    boolean add(Object obj);
     boolean addAll(Collection coll);

     2、删除:
     boolean remove(Object obj);
     boolean removeAll(Collection coll);
     void clear();

     3、判断:
     boolean contains(Object obj);
     boolean containsAll(Collection coll);
     boolean isEmpty();判断集合中是否有元素。

     4、获取:
     int size();
     Iterator iterator();
     取出元素的方式:迭代器。
     该对象必须依赖于具体容器,因为每一个容器的数据结构都不同,所以该迭代器对象是在容器中进行内部实现的,也就是iterator方法在每个容器中的实现方式是不同的。
     对于使用容器者而言,具体的实现不重要,只要通过容器获取到该实现的迭代器的对象即可,也就是iterator方法。
     Iterator接口就是对所有的Collection容器进行元素取出的公共接口。

     5、其他:
     boolean retainAll(Collection coll);取交集
     Object toArray();将集合转成数组
示例1:
  1. import java.util.*;

  2. public class CollectionDemo{
  3.       
  4.        public static void main(String[] args){
  5.             Collection coll = new ArrayList();
  6.             show(coll);

  7.             System.out.println( "---------------------------------" );

  8.             Collection c1 = new ArrayList();
  9.             Collection c2 = new ArrayList();
  10.             show(c1,c2);
  11.       }
  12.       
  13.        public static void show(Collection coll){
  14.              //1、添加元素,add
  15.             coll.add( "abc1");
  16.             coll.add( "abc2");
  17.             coll.add( "abc3");
  18.             System.out.println( "coll:" + coll);

  19.              //2、删除元素,remove
  20.             coll.remove( "abc2");//会改变集合的长度
  21.             System.out.println( "coll:" + coll);

  22.              //清空集合
  23.              //coll.clear();
  24.             System.out.println(coll.contains( "abc1"));
  25.       }

  26.        public static void show(Collection c1,Collection c2){
  27.              //给c1添加元素
  28.             c1.add( "abc1");
  29.             c1.add( "abc2");
  30.             c1.add( "abc3");
  31.             c1.add( "abc4");

  32.              //给c2添加元素
  33.             c2.add( "abc2");
  34.             c2.add( "abc6");
  35.             c2.add( "abc7");

  36.             System.out.println( "c1:" + c1);
  37.             System.out.println( "c2:" + c2);

  38.              //演示addAll
  39.              //将c2中的元素添加到c1中
  40.             c1.addAll(c2);

  41.              //演示removeAll
  42.              //从c1集合中删除与c2集合相同的元素
  43.              boolean b = c1.removeAll(c2);
  44.              System.out.println( "removeAll:" + b);

  45.              //演示containsAll
  46.              boolean b1 = c1.containsAll(c2);
  47.              System.out.println( "containsAll:" + b1);

  48.              //演示retainAll
  49.              //取交集,保留和指定的集合相同的元素
  50.              boolean b2 = c1.retainAll(c2);
  51.              System.out.println( "c1、c2交集:" + c1);
  52.       }
  53. }

运行结果:
示例2:

  1. import java.util.ArrayList;
  2. import java.util.Collection;
  3. import java.util.Iterator;

  4. public class IteratorDemo{
  5.        public static void main(String[] args){
  6.             Collection coll = new ArrayList();

  7.             coll.add( "abc1");
  8.             coll.add( "abc2");
  9.             coll.add( "abc3");
  10.             coll.add( "abc4");

  11.             System.out.println(coll);

  12.              //使用了Collection中的iterator()方法。调用集合中的迭代器方法,是为了获取集合中的迭代器对象。
  13.             Iterator it1 = coll.iterator();

  14.              while(it1.hasNext()){
  15.                   System.out.println(it1.next());
  16.             }

  17.              //for循环结束,Iterator变量内存释放,更高效
  18.              for(Iterator it2 = coll.iterator();it2.hasNext();){
  19.                   System.out.println(it2.next());
  20.             }
  21.       }
  22. }

运行结果:


List、Set
 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集合可以完成对元素的增删改查。


 示例1:
  1. import java.util.ArrayList;
  2. import java.util.List;

  3. public class ListDemo{
  4.        public static void main(String[] args){
  5.             List list = new ArrayList();
  6.             show(list);
  7.       }

  8.        public static void show(List list){
  9.              //添加元素
  10.             list.add( "abc1" );
  11.             list.add( "abc2" );
  12.             list.add( "abc3" );

  13.             System.out.println(list);

  14.              //插入元素
  15.             list.add(1, "abc2" );

  16.              //删除元素
  17.             System.out.println( "remove:" + list.remove(2));

  18.              //修改元素
  19.             System.out.println( "set:" + list.set(1,"abc8" ));

  20.              //获取元素:
  21.             System.out.println( "get:" + list.get(0));
  22.            
  23.              //获取子列表
  24.             System.out.println( "sublist:" + list.subList(1,2));

  25.             System.out.println(list);
  26.       }
  27. }

运行结果:
 示例2:
  1. import java.util.ArrayList;
  2. import java.util.Iterator;
  3. import java.util.List;
  4. import java.util.ListIterator;

  5. public class ListDemo{
  6.        public static void main(String[] args){
  7.             List list = new ArrayList();
  8.             show(list);
  9.       }

  10.        public static void show(List list){
  11.             list.add( "abc1");
  12.             list.add( "abc2");
  13.             list.add( "abc3");
  14.             list.add( "abc4");
  15.             
  16.             Iterator it = list.iterator();
  17.              while(it.hasNext()){
  18.                   System.out.println( "next:" + it.next());
  19.             }

  20.              //list特有的取出元素的方式之一
  21.              for(int x = 0; x < list.size(); x++){
  22.                   System.out.println( "get:" + list.get(x));
  23.             }
  24.       }
  25. }
运行结果:

 示例3: 
  1. import java.util.ArrayList;
  2. import java.util.Iterator;
  3. import java.util.List;

  4. public class ListDemo{
  5.        public static void main(String[] args){
  6.             List list = new ArrayList();

  7.             list.add( "abc1");
  8.             list.add( "abc2");
  9.             list.add( "abc3");

  10.             System.out.println( "list:" + list);

  11.             Iterator it = list.iterator();
  12.             
  13.              while(it.hasNext()){
  14.                   Object obj = it.next();

  15.                    if(obj.equals("abc2" )){
  16.                         list.add( "abc9");
  17.                   } else{
  18.                         System.out.println( "next:" + obj);
  19.                   }
  20.                   System.out.println(list);
  21.             }
  22.       }
  23. }
运行结果:

 在迭代器过程中,不要使用集合操作元素,容易出现异常:java.util.ConcurrentModificationException。
    可以使用Iterator接口的子接口ListIterator来完成在迭代中对元素进行更多的操作。
示例4:
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.ListIterator;

  4. public class ListDemo{
  5.        public static void main(String[] args){
  6.             List list = new ArrayList();

  7.             list.add( "abc1");
  8.             list.add( "abc2");
  9.             list.add( "abc3");

  10.             System.out.println( "list:" + list);

  11.             ListIterator it = list.listIterator(); //获取列表迭代器对象
  12.              //它可以实现在迭代过程中完成对元素的增删改查。
  13.              //注意:只有list集合具备该迭代功能。

  14.              while(it.hasNext()){
  15.                   Object obj = it.next();

  16.                    if(obj.equals("abc3" )){
  17.                         it.add( "abc9");
  18.                   }
  19.             }

  20.             System.out.println( "hasNext:" + it.hasNext());
  21.             System.out.println( "hasPrevious:" + it.hasPrevious());
  22.             
  23.              while(it.hasPrevious()){
  24.                   System.out.println( "previous:" + it.previous());
  25.             }
  26.             System.out.println( "list:" + list);
  27.       }
  28. }
运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值