集合Collection、遍历集合元素的方法(一)

目录

一、集合框架的概述

二、集合框架

三、Collection接口中声明的方法的测试

四、JDK 5.0 新增了foreach循环,用于遍历集合,数组

五、使用迭代器Interator接口遍历集合元素


 

一、集合框架的概述

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

2.1 数组在存储多个数据方面的特点:
      >—旦初始化以后,其长度就确定了。
      >数组一旦定义好,其元素的类型也就确定了。我们也就只能操作指定类型的数据了。比如: String[] arr;int[] arr1;
2.2 数组在存储多个数据方面的缺点:
      >—旦初始化以后,其长度就无法修改
      >数组中提供的方法非常有限,对于添加、删除、插入数据非常不便,效率不高
      >数组中没有提供一个现成的属性或方法来表示存放的元素个数
      >数组存放数据的特点:有序、可重复。对于无序不可重复的需求,不能满足

二、集合框架

 /----Collection接口:单列集合,用来存储的一个一个的对象
       /----List接口:存储有序的,可重复的数据(动态数组)
              /----ArrayList、LinkedList、Vector
       /----Set接口:存储的无序的,不可重复的数据(类似于高中的集合)
              /----HashSet、LinkedHashSet、TreeSet
/----Map接口:双列集合,用来存储一对一对的数据。(key、value)(函数)
              /----HashMap、LinkedHashMap、TreeMap、Hashtable、Properties

三、Collection接口中声明的方法的测试

要求:Collection接口的实现类要重写equals(),判断是否包含某个元素,删除元素,查找,都需要用到

  contains(Object obj):判断集合中是否包含这个obj
  containsAll(Collection coll1):判断形参coll1中的所有元素是否都在当前集合中
  remove(Object obj):从当前集合中删除obj元素
  removeAll(Collection coll1):差集:从当前集合中删除coll1中的所有元素
  retainAll(Collection coll1):交集:保留当前集合和coll1中共有的元素
  equals(Collection coll1):判断两个集合的元素是否相等,需要考虑集合是有序的还是无序的
  hashCode():返回当前对象的哈希值
  toArray():集合--->数组
  数组--->集合:调用Arrays类的asList静态方法
public class CollectionTest1 {
    @Test
    public void test1() {
        Collection c = new ArrayList();
        c.add(123);
        c.add(456);
        c.add(new String("haiyu"));
        c.add(new Person(12, "haiuyu"));
        c.add(false);

        boolean contains = c.contains(123);
        System.out.println(contains);//true

        System.out.println(c.contains(new String("haiyu")));//true
        System.out.println(c.contains(new Person(12, "haiuyu")));//false
//        总结:contains相当于equals,String中重写过equals,比较的是内容。自定义的类中没有重写过。

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

        System.out.println("*************");
        c.remove(123);
        System.out.println(c);

        c.removeAll(coll1);
        System.out.println(c);
    }


    @Test
    public void test2() {
        Collection c = new ArrayList();
        c.add(123);
        c.add(456);
        c.add(new String("haiyu"));
        c.add(new Person(12, "haiuyu"));
        c.add(false);

        Collection coll1 = Arrays.asList(123, 456, 38383, 3);

        c.retainAll(coll1);
        System.out.println(c);

        System.out.println(c.hashCode());//5230

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

        List<int[]> ints = Arrays.asList(new int[]{123, 456});
        System.out.println(ints.size());//1

        List<Integer> integers = Arrays.asList(new Integer[]{123, 456});
        System.out.println(integers.size());//2
    }
}

四、JDK 5.0 新增了foreach循环,用于遍历集合,数组

public class ForTest {
    @Test
    public void test1() {
        Collection c = new ArrayList();
        c.add(123);
        c.add(456);
        c.add(new String("haiyu"));
        c.add(new Person(12, "haiuyu"));
        c.add(false);
        //for(集合元素的类型 局部变量:集合对象)           for(数组元素的类型 局部变量 : 数组本身)
        //内部仍然调用的迭代器
        for (Object obj:c){
            System.out.println(obj);
        }
    }

五、使用迭代器Interator接口遍历集合元素

1、内部有两个方法:hasNext()   next()
2、内部还有一个remove()方法,可以在遍历的时候,删除集合中的元素,此方法不同于集合直接调用remove()
public class IteratorTest {
    @Test
    public void test1(){
        Collection c = new ArrayList();
        c.add(123);
        c.add(456);
        c.add(new String("haiyu"));
        c.add(new Person(12,"haiuyu"));
        c.add(false);

        Iterator iterator = c.iterator();//可以理解为有了一个指针,指向集合第一个元素之前的一个空的地方
//        方式一:
//        System.out.println(iterator.next());
//        System.out.println(iterator.next());
//        System.out.println(iterator.next());
//        System.out.println(iterator.next());
//        System.out.println(iterator.next());
//        System.out.println(iterator.next());         NoSuchElementException
//        方式二:不推荐
//        for (int i = 0; i < c.size(); i++) {
//            System.out.println(iterator.next());
//        }
//        方式三:推荐
        while (iterator.hasNext()){//这里不能是c.iterator.hasNext(),因为每次调用都会是一个新的指针结构出现
            System.out.println(iterator.next());
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

java塑造中...

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值