Java Collection集合和List集合

目录

思维导图

1 Collection

1.1 集合知识回顾

1.2 集合类体系结构

1.3 Collection 集合概述和使用

1.4 Collection 集合常用方法

1.5 Collection 集合的遍历

1.6 集合的使用步骤

案例:Collection 集合存储对象并遍历

2 List

2.1 List集合的概述和特点

 2.2 List集合特有方法

 案例:List集合存储学生对象并遍历

2.3 并发修改异常

2.4 ListIterator

2.5 增强for循环

案例:List集合存储学生对象用三种方式遍历

2.6 数据结构

2.7 常见的数据结构之栈

 2.8 常见数据结构之队列

2.9 常见的数据结构之数组

2.10 常见的数据结构之链表 

2.11 List集合子类特点

案例:ArrayList集合存储学生对象用三种方式遍历

2.12 LinkedList集合的特有功能

​编辑


思维导图

 

1 Collection

1.1 集合知识回顾

集合类的特点:提供一种存储空间可变的存储模型,存储的数据容量可以随时发生改变

1.2 集合类体系结构

1.3 Collection 集合概述和使用

Collection集合概述

● 是单列集合的顶层接口,它表示一组对象,这些对象也称为Collection的元素

● JDK 不提供此接口的任何直接实现,它提供更具体的子接口(如Set和List)实现

创建Collection集合对象

● 多态的方式

● 具体的实现类 ArrayList

示例代码:

public class test {
    public static void main(String[] args) {
        //创建Collection集合对象
        Collection<String> c = new ArrayList<String>();

        //添加元素,boolean add(E e)
        c.add("hello");
        c.add("world");
        c.add("java");

        //输出结果
        System.out.println(c);
    }
}

1.4 Collection 集合常用方法

补充:Alt + 7 可以打开一个 窗口,可以看到所有类的信息

示例代码:

public class Test_01 {
    public static void main(String[] args) {
        //创建一个Collection集合对象
        Collection<String> c = new ArrayList<String>();

        //boolean add(E e):添加元素
        c.add("hello");
        c.add("world");
        c.add("java");

        //boolean remove(Object o):从集合中移除指定的元素
//        System.out.println(c.remove("world"));
//        System.out.println(c.remove("javaee"));

        //void clear():清空集合中的元素
//        c.clear();

        //boolean contains(Object o):判断集合中是否存在指定的元素
//        System.out.println(c.contains("world"));
//        System.out.println(c.contains("javaee"));

        //boolean isEmpty():判断集合是否为空
//        System.out.println(c.isEmpty());

        //int size():集合的长度,也是集合中元素的个数
        System.out.println(c.size());

        //输出集合
        System.out.println(c);
    }
}

1.5 Collection 集合的遍历

Iterator:迭代器,集合的专用遍历式

● Iterator<E> iterator() :返回集合中元素的迭代器,通过集合的iterator()方法得到

● 迭代器是通过集合的iterator()方法得到的,所有我们说它是依赖于集合而存在的

Iterator中的常用方法

● E next():判断集合中是否存在指定的元素

● boolean hasNext():如果迭代具有更多元素。则返回true

示例代码:

public class Test_02 {
    public static void main(String[] args) {
        //创建Collection集合对象
        Collection<String> c = new ArrayList<String>();

        //添加元素,boolean add(E e)
        c.add("hello");
        c.add("world");
        c.add("java");

        Iterator<String> it = c.iterator();

        while(it.hasNext()){
            String s = it.next();
            System.out.println(s);
        }
    }
}

1.6 集合的使用步骤

案例:Collection 集合存储对象并遍历

需求:创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合 

示例代码:

//学生类
public class Student {
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

//测试类
public class Test {
    public static void main(String[] args) {
        Collection<Student> students = new ArrayList<Student>();

        Student s1 = new Student("jack",20);
        Student s2 = new Student("mary",21);
        Student s3 = new Student("jane",22);

        students.add(s1);
 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值