Java 集合与迭代器全攻略

集合(Collection)

为什么不用数组而要用集合呢?

数组弊端:
1. 只能添加相同类型的元素(基本数据类型 和 引用数据类型)
2. 长度一旦确定就不能改变,要添加超出数组元素个数的元素 操作比较复杂
集合好处
1. 能添加不能类型的元素
注意:集合中只能添加 引用数据类型 (只能添加对象类型)
2. 长度可变

集合一些方法代码示例:

// rawtypes保持 原有类型   unchecked 不检查插入数据类型
// 放到类上 整个类不写泛型 都不会报黄
@SuppressWarnings({ "rawtypes", "unchecked" })
public class Demo02 {

    public static void main(String[] args) {
        // 创建一个集合 多态的声明方法   
        //fun1();
        //fun2();
        //fun3();
        //fun4();
        //fun5();
        //fun7();       
    }
    /**
     * removeAll
     */
    public static void fun7() {
        Collection collection = new ArrayList();
        Collection collection1 = new ArrayList();
        collection.add("a");
        collection.add("b");
        collection.add("c");
        collection1.add("a");
        collection1.add("y");
        collection1.add("z");
        // 把两个集合的交集取出来 保存在1集合中 谁调的方法保存在谁那
        // 如果1集合和2集合 在求出交集时候 放到 1集合中 如果1集合和原来对比发生变化返回 true 如果不发生变化返回false
        boolean removeAll = collection.retainAll(collection1);
        System.out.println(collection);
        System.out.println(collection1);
        System.out.println(removeAll);
    }

    /**
     * 
     */
    public static void fun5() {
        Collection collection = new ArrayList();
        Collection collection1 = new ArrayList();
        collection.add("a");
        collection.add("b");
        collection.add("c");
        collection1.add("a");
        collection1.add("y");
        collection1.add("z");
        // 将两个集合的交集删掉 谁调用这个方法就删除谁的元素 另外的保持不变
        // 只删除交集 重复也可以删 
        boolean removeAll = collection.removeAll(collection1);
        System.out.println(collection);
        System.out.println(collection1);
        System.out.println(removeAll);
    }

    /**
     * addAll 方法
     */
    public static void fun4() {
        Collection collection = new ArrayList();
        Collection collection1 = new ArrayList();
        collection.add("a");
        collection.add("a");
        collection1.add("ab");
        collection1.add("c");
        collection1.add("a");
        //将2集合中的元素 每一个元素取出来 添加到1集合末尾
        // 给集合1添加一个元素 元素是2集合
        //collection.add(collection1); 
        collection.addAll(collection1);
        System.out.println(collection);
        System.out.println(collection1);
    }

    /**
     * 集合强转
     */
    public static void fun3() {
        Collection collection = new ArrayList();
        collection.add(new Student("james", 18));
        collection.add(new Student("Tom", 18));
        collection.add(new Student("jelly", 18));
        // 集合转数组(向上转型)
        Object[] array = collection.toArray();
        //遍历数组
        for (int i = 0; i < array.length; i++) {
            // array[i] 直接从数组中取出来是Object类型 要使用 student类中的方法需要 向下转型 但是必须是这个类型才能强转
            // 对象调方法 强转注意:把每一个数组中每一个对象进行强转 而不是把保存对象的容器(数组) 转化
            Student student = (Student)array[i];
            System.out.println(student.getName());

        }
    }

    /**
     *  打印字符串
     */
    public static void fun2() {
        Collection collection = new ArrayList();    
        collection.add("a");
        collection.add("b");
        collection.add("c");
        collection.add("d");
        // 遍历集合中的每一个元素
        Object[] array = collection.toArray();
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);

        }
    }

    /**
     * 集合的基本方法
     * @param collection
     */
    public static void fun1() {
        //添加方法
        Collection collection = new ArrayList();    
        collection.add("a");
        collection.add("b");
        collection.add("c");
        // 当向集合中添加基本数据类型的时候 系统会自动装箱 把基本数据类型变成它的包装类的对象
        collection.add(10);
        System.out.println(collection);
        // 获取集合的长度
        System.out.println(collection.size());
        // 判断是否包含某个元素
        boolean contains = collection.contains("b");
        System.out.println(contains);
        // 从集合中删除一个元素
        collection.remove("b");

        //操作的是 原集合 是集合本身
        // 打印集合
        System.out.println(collection);
        // 判断集合 是否为空
        boolean empty = collection.isEmpty();
        System.out.println(empty);
        // 清空数组
        collection.clear();
        System.out.println(collection);
    }
}

迭代器(遍历)

对 collection 进行迭代的迭代器

常用方法举例:

@SuppressWarnings({ "rawtypes", "unchecked" })
public class Demo03 {
    public static void main(String[] args) {
        //fun1();
        //fun2();   
        //fun3();       
    }
    /**
     * 
     */
    public static void fun3() {
        Collection collection = new ArrayList();
        collection.add(new Student("James", 18));
        collection.add(new Student("Tom", 17));
        collection.add(new Student("jelly", 19));

        Iterator iterator = collection.iterator();
        while (iterator.hasNext()) {

            Object next = iterator.next();
            // 强转成student类
            Student student =(Student)next;
            System.out.println(student.getName());
        }
    }
    /**
     * 迭代遍历集合
     */
    public static void fun2() {
        Collection collection = new ArrayList();
        // 迭代是 有一个指针 指向首位置
        // 每调用一次next方法 指针向下移一格
        collection.add("a");
        collection.add("b");
        collection.add("c");
        collection.add("d");
        Iterator iterator = collection.iterator();
        // 如果有元素就继续获取 没元素停止循环
        // 注意: 迭代时 循环中只能使用一次next() 方法
        while (iterator.hasNext()) {
            Object next = iterator.next();
            System.out.println(next);
        }
    }
    /**
     * 迭代器
     */
    public static void fun1() {
        //测试迭代器中的方法
        Collection collection = new ArrayList();
        collection.add("a");
        collection.add("b");
        collection.add("c");
        collection.add("d");

        //获取集合中的迭代器
        Iterator iterator = collection.iterator();
        // 先判断集合中是否有元素 
        boolean hasNext = iterator.hasNext();
        System.out.println(hasNext);
        // 从集合中取出元素
        Object next = iterator.next();
        System.out.println(next);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,集合类库提供了一个统一的迭代器接口 `Iterator`,用于遍历集合中的元素。通过迭代器,我们可以按照特定顺序访问集合中的元素,而不需要了解底层数据结构的具体实现。 使用迭代器遍历集合的一般步骤如下: 1. 获取集合对象的迭代器:通过调用集合类的 `iterator()` 方法获取迭代器对象。 2. 使用 `hasNext()` 方法检查是否还有下一个元素。 3. 使用 `next()` 方法获取下一个元素。 4. 对当前元素进行操作。 5. 重复步骤 2-4 直到遍历完所有元素。 以下是一个示例代码,展示如何使用迭代器遍历一个 ArrayList 集合: ```java import java.util.ArrayList; import java.util.Iterator; public class IteratorExample { public static void main(String[] args) { ArrayList<String> list = new ArrayList<>(); list.add("Apple"); list.add("Banana"); list.add("Orange"); // 获取迭代器 Iterator<String> iterator = list.iterator(); // 遍历集合 while (iterator.hasNext()) { String element = iterator.next(); System.out.println(element); } } } ``` 上述代码中,通过调用 `list.iterator()` 方法获取了 ArrayList 的迭代器对象。然后使用 `hasNext()` 方法检查是否还有下一个元素,再使用 `next()` 方法获取下一个元素。最后,我们可以对每个元素进行操作,这里简单地将其打印到控制台。 需要注意的是,在使用迭代器遍历集合的过程中,如果在迭代时对集合进行了修改(如添加、删除元素),则需要使用迭代器的 `remove()` 方法来删除元素,以避免出现并发修改异常。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值