集合、集合的顶层接口Collection、迭代器接口、迭代器遍历集合

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013317445/article/details/81915451

集合

集合的特点

集合只能存储对象(引用类型),集合长度可变。集合可以存储不同类型的对象。
java提供了多种集合类。

集合的继承体系

这里写图片描述
体系老大:Collection
自上至下从抽象到具体,最底层是具体的类。


Collection——集合的顶层接口

Collection的功能

要import java.util.Collection;

  • 添加功能
    boolean add(Object obj):添加一个元素
    boolean addAll(Collection c):添加一个集合的元素
  • 删除功能
    void clear():移除所有元素
    boolean remove(Object o):移除一个元素
    boolean removeAll(Collection c):移除一个集合的元素(是一个还是所有)
  • 判断功能
    boolean contains(Object o):判断此集合中是否包含指定的元素
    boolean containsAll(Collection c):判断此集合中是否包含指定的集合元素(是一个还是所有)
    boolean isEmpty():判断此集合是否为空
  • 获取功能
    Iterator<E> iterator()(重点)
  • 长度功能
    int size():元素的个数
    数组arr.length。字符串是s.length()方法。集合是c.size()方法。
  • 交集功能
    boolean retainAll(Collection c):仅保留此集合中包含在指定集合中的元素
  • 把集合转换为数组
    Object[] toArray()
import java.util.Collection;

public class CollectionDemo {
    public static void main(String[] args){
        Collection c1= new ArrayList();//接口不能实例化。所以采用向下转型
        //添加元素 这里是String对象
        c1.add("just");
        c1.add("a");
        c1.add("sec.");
        System.out.println("add后:"+c1);//[just, a, sec.]

        Collection c2= new ArrayList();
        //添加元素
        c2.add("get");
        c2.add("out,");
        c2.add("Alax.");
        System.out.println("add后:"+c2);//[get, out,, Alax.]

        //将c2中的所有元素添加到c1
        c1.addAll(c2);
        System.out.println("addAll后的c1:"+c1);//[just, a, sec., get, out,, Alax.]

        //从c1中移除“Alax”元素
        c1.remove("Alax.");
        System.out.println("remove后的c1:"+c1);//[just, a, sec., get, out,]

        //从c1中移除包含在c2中的所有元素
        c1.removeAll(c2);
        System.out.println("removeAll后的c1:"+c1);//[just, a, sec.]

        //查看c1中是否包含元素“uuu”
        System.out.println(c1.contains("uuu"));//false  //包含返回true,不包含则返回false
        //查看c1中是否包含c2中的所有元素
        System.out.println(c1.containsAll(c2));//false

        //查看c1集合是否为空
        System.out.println(c1.isEmpty());//false

        //查看c1中的元素个数
        int size= c1.size();
        System.out.println(size);//3

        c1.addAll(c2);
        c1.add("a");
        System.out.println(c1);//[just, a, sec., get, out,, Alax., a]

        //仅保留c1中包含在c3中的元素
        Collection c3= new ArrayList();
        c3.add("a");
        c3.add("get");
        c3.add("just");
        c1.retainAll(c3);
        System.out.println(c1);//[just, a, get, a]

        //清楚c3中的所有元素
        c3.clear();
        System.out.println(c3);//[]

    }

}

集合的遍历

集合的遍历之转成数组进行遍历

import java.util.Collection;
import java.util.ArrayList;

 //将集合转成数组
        Object[] carray= c1.toArray(); //注意:这个有向上转型 里面每个元素可以这样理解Object s=new String("just")
        System.out.println(carray);//[Ljava.lang.Object;@1b6d3586
        System.out.println(carray[0]);//just
        System.out.println("数组的长度:"+carray.length);//4
        System.out.println(carray.getClass());//Object
        System.out.println(carray[0].getClass());//String

        //集合转成数组,进行遍历
        for(int index=0; index< carray.length; index++){
            System.out.print(carray[index]+" "); //just a get a 
        }
import java.util.Collection;
import java.util.ArrayList;

public class StudentDemo {
    public static void main(String[] args){
        Student s1= new Student("lili",11);
        Student s2= new Student("jack",11);
        Student s3= new Student("lijian",12);
        Student s4= new Student("rose",10);
        Student s5= new Student("linyi",10);

        //创建集合
        Collection s= new ArrayList();
        //集合中添加学生对象
        s.add(s1);
        s.add(s2);
        s.add(s3);
        s.add(s4);
        s.add(s5);

        //转成数组后,遍历集合
        //Student[] sarray=s.toArray();//Error: java: 不兼容的类型: java.lang.Object[]无法转换为Collection.Student[]
        Object[] sarray=s.toArray();
        System.out.println(sarray.getClass().getName());//Object
        System.out.println(sarray[0].getClass().getName());//Student

        for(int index=0; index< sarray.length; index++){
            //现在要使用Object的子类Student特有的功能。
            //要向下转型(之前有向上转型 ,装父亲)
            Student ss=(Student)sarray[index];
            System.out.println(ss.getName()+"---"+ss.getAge());
        }
    }
}

迭代器

Iterator接口:
迭代器:集合专有遍历方式。
是集合获取元素的方式。
迭代器是 依赖于集合而存在的。
迭代器定义的是一个接口,它的真正的实现类在:在真正的具体子类中,以内部类的方式体现。
Iterator的功能(使用前要import java.util.Iterator;):
boolean hasNext():如果仍有元素可以迭代,则返回 true。
E next():返回迭代中的下一个元素
default void remove():从迭代器指向的 collection 中移除迭代器返回的最后一个元素

Collection有个功能:Iterator iterator()

集合的遍历之迭代器遍历

//迭代器遍历1
        Iterator it= s.iterator();//s.iterator()编译看左边运行看右边。返回的是子类对象new Itr(); 这里是多态
        while(it.hasNext()){
            System.out.println(it.next());
        }
        /*        
        输出结果(Student类里重写了toString()方法):
            Student{name='lili', age=11}
            Student{name='jack', age=11}
            Student{name='lijian', age=12}
            Student{name='rose', age=10}
            Student{name='linyi', age=10}
        */
//迭代器遍历2
//如果我要调用Student的getName方法
        Iterator it= s.iterator();
        while(it.hasNext()){
            Student ss= (Student)it.next();
            System.out.println(ss.getName()+"---"+ss.getAge());
        }
        /*
        输出结果:
            lili---11
            jack---11
            lijian---12
            rose---10
            linyi---10
        */

集合的使用步骤

  1. 创建集合对象
  2. 创建元素对象
  3. 把元素添加到集合
  4. 遍历集合

遍历集合的步骤

  1. 通过集合对象创建迭代器对象:如,Iterator it= c.iterator(); c为集合对象,it为迭代器对象
  2. 通过迭代器对象的hasNext()方法判断是否还有元素
  3. 通过next()方法获取元素并移动到下一个位置

猜你喜欢

转载自blog.csdn.net/u013317445/article/details/81915451
今日推荐