java集合与数组、Collection接口、Iterator接口与foreach循环

一、数组与集合

1.集合与数组存储数据概述:

集合、数组都是对多个数据进行存储操作的结构,简称java容器。
说明:此时的存储,主要指的是内存层面的存储,不涉及到持久化的存储(.txt,.jpg,.avi,数据库中)

2.数组存储的特点:

> 一旦初始化以后,其长度就确定了。
> 数组一旦定义好,其元素的类型也就确定了。比如:String[] arr;int[] arr1;

3.数组存储的弊端:

> 一旦初始化以后,其长度就不可修改。
> 数组中提供的方法非常有限,对于添加、删除、插入数据等操作,非常不便。同时效率不高。
> 获取数组中实际元素的个数的需求,数组没有现成的属性或方法可用
> 数组存储数据的特点:有序、可重复。对于无序、不可重复的需求,不能满足。

4.集合存储的优点:

解决数组存储数据方面的弊端

 

二、Collection接口

1.单例集合框架结构

|-----Collection接口;单例集合,用来存储一个一个的对象
* |----List接口;存储有序的、可重复的数据。 “动态”数组
* |----ArrayList、LinkedList、Vector
* |----Set接口:存储有序的、不可重复的数据 高中讲的“集合”
* |----HashSet、LinkedHashSet、TreeSet
对应图示:
2.Collection接口常用方法:
@Test
    public void test1(){
        Collection coll = new ArrayList();
        //add(Object e):将元素e添加到集合coll中
        coll.add("AA");
        coll.add("BB");
        coll.add(123);
        coll.add(new Date());

        //size():获取添加的元素的个数
        System.out.println(coll.size());

        //addAll()(Collection coll1):将coll1集合中的元素添加到当前的集合中
        Collection coll1 = new ArrayList();
        coll1.add(456);
        coll1.add("cc");
        coll.addAll(coll1);

        System.out.println(coll.size());
        System.out.println(coll);

        //clear():清空集合元素
        coll.clear();

        //isEmpty():判断当前集合是否为空
        System.out.println(coll.isEmpty());

    }

 @Test
    public void test1(){
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new String("Tom"));
        coll.add(false);
//        Person p = new Person("Jerry",13);
//        coll.add(p);
        coll.add(new Person("Jerry",13));

        //contains(Object obj):判断当前集合中是否包含obj
        //我们在判断时会调用obj对象所在类的equals()。
        boolean contains = coll.contains(123);
        System.out.println(contains);
        System.out.println(coll.contains(new String("Tom")));
//        System.out.println(coll.contains(p));
        System.out.println(coll.contains(new Person("Jerry", 13)));

        //2.containsAll(Collection coll1):判断形参coll1中的所有元素是否都存在于当前集合中。
        Collection coll1 = Arrays.asList(123,456);
        System.out.println(coll.containsAll(coll1));

    }
    @Test
    public void test2(){
        //3.remove(Object obj):从当前集合中移除obj元素。
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new String("Tom"));
        coll.add(new Person("Jerry",13));
        coll.add(false);
        coll.remove(123);
        System.out.println(coll);

        coll.remove(new Person("Jerry",13));
        System.out.println(coll);

        //4.removeAll(Collection coll1):差集:从当前集合中移除coll1中所有的元素
        Collection coll1 = Arrays.asList(123,4567);
        coll.removeAll(coll1);
        System.out.println(coll);
    }
    @Test
    public void test3(){
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new String("Tom"));
        coll.add(new Person("Jerry",13));
        coll.add(false);

        //5.retainAll(Collection coll1):交集:获取当前集合和coll1集合的交集,并返回给当前集合
//        Collection coll1 = Arrays.asList(123,456,789);
//        coll.retainAll(coll1);
//        System.out.println(coll);

        //equals(Object obj):要想返回true,需要当前集合和形参集合的元素都相同
        Collection coll1 = new ArrayList();
        coll1.add(123);
        coll1.add(456);
        coll1.add(new String("Tom"));
        coll1.add(new Person("Jerry",13));
        coll1.add(false);
        System.out.println(coll.equals(coll1));
    }
    @Test
    public void test4(){
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new String("Tom"));
        coll.add(new Person("Jerry",13));
        coll.add(false);
        //hashCode():返回当前对象的哈希值
        System.out.println(coll.hashCode());

        //8.集合 ---->数组:toArray():
        Object[] arr = coll.toArray();
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
        //拓展:数组 ---->集合:调用Array类的静态方法asList()
        List<String> list = Arrays.asList(new String[]{"aa", "bb", "cc"});
        System.out.println(list);

        //9.iterator():返回Iterator接口的实例,用于遍历集合元素。放在IteratorTest.java中测试

    }

3.Collection集合与数组间的转换

@Test
    public void test4(){
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new String("Tom"));
        coll.add(new Person("Jerry",13));
        coll.add(false);
        //hashCode():返回当前对象的哈希值
        System.out.println(coll.hashCode());

        //8.集合 ---->数组:toArray():
        Object[] arr = coll.toArray();
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
        //拓展:数组 ---->集合:调用Array类的静态方法asList()
        List<String> list = Arrays.asList(new String[]{"aa", "bb", "cc"});
        System.out.println(list);

        //9.iterator():返回Iterator接口的实例,用于遍历集合元素。放在IteratorTest.java中测试

    }

 

4.使用Collection集合存储对象,要求对象所属的类满足:

向Collection接口的实现类的对象中添加数据obj时,要求obj所在类重写equals()。

三、Iterator接口与foreach循环

1.遍历Collection的两种方式:
① 使用迭代器Iterator ②foreach循环(或增强for循环)
2.java.utils包下定义的迭代器接口:Iterator
2.1 说明:

 Iterator对象称为迭代器(设计模式的一种),主要用于遍历 Collection 集合中的元素。
 GOF给迭代器模式的定义为:提供一种方法访问一个容器(container)对象中各个元
素,而又不需暴露该对象的内部细节。 迭代器模式,就是为容器而生。

2.2 作用:

遍历集合Collection元素

2.3如何获取实例:

coll.iterator()返回一个迭代器实例

2.4遍历的代码实现:

@Test
    public void test1(){
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new String("Tom"));
        coll.add(new Person("Jerry",13));
        coll.add(false);

        Iterator iterator = coll.iterator();//方式二:不推荐
//        for (int i = 0; i < coll.size(); i++) {
//            System.out.println(iterator.next());
//        }
        //方式三:推荐
        //hasNext():判断是否还有下一个元素
        while (iterator.hasNext()){
            //next():①指针下移 ②将下移以后集合位置上的元素返回
            System.out.println(iterator.next());
        }
    }

2.5图示说明

 

 

 2.6 remove()的使用

//测试Iterator中的remove()
    //如果还未调用next()或在上一次调用 next 方法之后已经调用了 remove 方法,
    //再调用remove都会报IllegalStateException
//
内部定义了remove(),可以在遍历的时候,删除集合中的元素。此方法不同于集合直接调用remove()
    @Test
    public void test3(){
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new String("Tom"));
        coll.add(new Person("Jerry",13));
        coll.add(false);
        Iterator iterator = coll.iterator();
        //删除集合中“Tom”
        while (iterator.hasNext()){
            Object obj = iterator.next();
            if ("Tom".equals(obj)){
                iterator.remove();
            }
        }
        //遍历集合
        iterator = coll.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }

3.jdk5.0新特性 ----增强for循环:(foreach循环)

3.1 遍历集合举例:


@Test
    public void test1(){
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new String("Tom"));
        coll.add(new Person("Jerry",13));
        coll.add(false);

        //for(集合元素的类型 局部变量 : 集合对象)
        //
        for (Object obj : coll){
            System.out.println(obj);
        }
    }

3.2 说明:

内部仍然调用了迭代器

3.3遍历数组举例:
@Test
    public void test2(){
        int[] arr = new int[]{1,2,3,4,5,6};
        //for(数组元素的类型 局部变量 : 数组对象)
        for (int i : arr){
            System.out.println(i);
        }
    }

 

 
posted @ 2022-01-29 23:07  15python  阅读(60)  评论(0编辑  收藏  举报