“21天好习惯”第一期-21

本文介绍了Java集合框架中Collection接口的基础概念,重点讲解了它与数组的区别,并通过实例展示了如何使用ArrayList和LinkedList。还涵盖了List接口的使用,以及它们的实现类对比,适合初学者理解Java集合的灵活性和动态性。
摘要由CSDN通过智能技术生成

21天养成好习惯_第二十一天

今天学习Java的集合知识 参考资料: 千峰教育
(1): 概念:

对象的容器,定义了对多个对象进行操作的常用方法. 可实现数组的功能

(2): 和数组的区别
  1. 数组长度固定 , 集合长度不固定
  2. 数组可以存储基本类型和引用类型 , 集合只能存储引用类型
  3. 位置: java.util.*;
Collection 体系集合图

在这里插入图片描述

在这里插入图片描述

collection 接口的代码演示
package Collection_Study;

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

//Collection是一个接口
public class CollectionDemo {
    public static void main(String[] args) {
        //1.创建集合 (集合只能插入引用类型)
        Collection collection = new ArrayList();
        //2.添加元素(可以添加不同类型)
        collection.add("苹果");
        collection.add("西瓜");
        collection.add("榴莲");
        collection.add(10);
        System.out.println("元素个数: " + collection.size());
        System.out.println(collection);
        //2.删除元素
//        collection.remove("榴莲");
//        collection.clear();
//        System.out.println("删除自后: " + collection.size());

        //3.遍历元素(重点)
        System.out.println("------使用增强for--------");
        for (Object o : collection) {
            System.out.println(o);
        }
        //3.2 使用增强迭代器(迭代器也是一个接口)
        System.out.println("---------3.2 使用迭代器----------");
        Iterator it = collection.iterator();
        while (it.hasNext()){
            System.out.println(it.next());
            //若想在此循环中删除元素 只能调用迭代器的remove方法,而不能调用collection的remove方法
//            it.remove();
        }
//        System.out.println(collection.size());
        //4.判断
        System.out.println("---------判断-----------");
        System.out.println(collection.contains("西瓜"));
        System.out.println(collection.isEmpty());
    }
}

运行结果:
元素个数: 4
[苹果, 西瓜, 榴莲, 10]
------使用增强for--------
苹果
西瓜
榴莲
10
---------3.2 使用迭代器----------
苹果
西瓜
榴莲
10
---------判断-----------
true
false

List子接口

在这里插入图片描述

List的接口代码实例
package Collection_Study;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

/**
 * List子接口的使用
 * 特点: 1.有序有下标,2.可以重复
 */

public class ListDemo {
    public static void main(String[] args) {
        //1.先创建对象
        List list = new ArrayList<>();
        //2.添加元素
        list.add("苹果");
        list.add("小米");
        list.add(0,"华为");
        System.out.println("元素个数:  " +list.size());
        System.out.println(list.toString());

        //3.删除元素
//        list.remove("苹果");
//        list.remove(0);
//        System.out.println("删除之后: " + list.toString());

        //4.遍历
        //4.1 使用for遍历
        System.out.println("使用for遍历");
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
        //4.2 使用增强for遍历
        System.out.println("使用增强for遍历");
        for (Object o : list) {
            System.out.println(o);
        }
        //4.3 使用迭代器
        Iterator it = list.iterator();
        System.out.println("使用迭代器");
        while (it.hasNext()){
            System.out.println((it.next()).toString());
        }
        //4.4 使用列表迭代器
        //****和Iterator的区别: ListIterator 可以向前或向后遍历,添加,删除修改元素
        ListIterator listIterator = list.listIterator();
        System.out.println("4.4使用列表迭代器从前往后遍历");
        while (listIterator.hasNext()){
            System.out.println(listIterator.nextIndex() + " " + listIterator.next().toString());
        }
        System.out.println("4.5使用列表迭代器从后往前遍历");
        while (listIterator.hasPrevious()){
            System.out.println(listIterator.previousIndex() + " " + listIterator.previous().toString());
        }

    }
}

运行结果:

元素个数:  3
[华为, 苹果, 小米]
使用for遍历
华为
苹果
小米
使用增强for遍历
华为
苹果
小米
使用迭代器
华为
苹果
小米
4.4使用列表迭代器从前往后遍历
0 华为
1 苹果
2 小米
4.5使用列表迭代器从后往前遍历
2 小米
1 苹果
0 华为

List实现类的对比

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值