集合框架05:List接口使用、List实现类、ArrayList使用

视频链接:13.11 ArrayList使用_哔哩哔哩_bilibilihttps://www.bilibili.com/video/BV1zD4y1Q7Fw?p=11&vd_source=b5775c3a4ea16a5306db9c7c1c1486b5

1.List接口使用代码举例

java 复制代码
package com.yundait.Demo01;

import java.util.ArrayList;
import java.util.List;

public class ListDemo02 {
    public static void main(String[] args){
        //创建集合
        List list = new ArrayList<>();

        //1.添加数字数据(自动装箱)
        list.add(20);
        list.add(30);
        list.add(40);
        list.add(50);
        list.add(60);
        System.out.println("元素个数" + list.size());
        System.out.println(list.toString());

        //2.删除操作
        //list.remove(0);通过下标方式删除
        list.remove(new Integer(20));
        System.out.println("元素个数" + list.size());
        System.out.println(list.toString());

        //3.subList方法;返回子集合,含头不含尾(左闭右开)
        List subList = list.subList(1, 3);
        System.out.println(subList.toString());
    }

}

输出结果:

2.List实现类介绍

3.ArrayList使用代码示例

重写Student类的equals方法:

java 复制代码
package com.yundait.Demo01;

/**
 *
 *学生类
 * @author zhang
 */
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;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object obj) {
        //1.判断是不是同一个对象
        if (this==obj){
            return true;
        }

        //2.判断是否为空
        if (obj==null){
            return false;
        }

        //3.判断是否为Student类型
        if (obj instanceof Student){

            Student s = (Student)obj;
            //4.比较属性
            if (this.name.equals(s.getName())&&this.age == s.getAge()){
                return true;
            }
        }
        
        //5.不满足条件返回false
        return false;
    }
}
java 复制代码
package com.yundait.Demo01;

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

public class ArrayListDemo01 {
    public static void main(String[] args) {
        //创建集合
        ArrayList arrayList = new ArrayList<>();

        //1.添加元素
        Student s1 = new Student("刘德华",20);
        Student s2 = new Student("郭富城",22);
        Student s3 = new Student("梁朝伟",24);
        Student s4 = new Student("甄子丹",18);
        arrayList.add(s1);
        arrayList.add(s2);
        arrayList.add(s3);
        arrayList.add(s4);
        System.out.println("元素个数" + arrayList.size());
        System.out.println(arrayList.toString());

        //2.删除元素
        //arrayList.remove(s1);
        arrayList.remove(new Student("刘德华",20)); //要想以这种方式删除需要重写equals方法
        System.out.println("删除之后元素个数" + arrayList.size());
        System.out.println(arrayList.toString());

        //3.遍历
        //3.1 使用Iterator进行遍历
        System.out.println("--------3.1 使用Iterator进行遍历---------");
        Iterator it = arrayList.iterator();
        while (it.hasNext()){
            Student s = (Student)it.next();
            System.out.println(s.toString());
        }

        //3.2使用ListIterator进行正序遍历
        System.out.println("----------3.2使用ListIterator进行正序遍历-----------");
        ListIterator lit= arrayList.listIterator();
        while(lit.hasNext()){
            Student s = (Student) lit.next();
            System.out.println(s.toString());
        }

        //3.3使用ListIterator进行逆序遍历
        System.out.println("----------3.2使用ListIterator进行逆序遍历-----------");
        while (lit.hasPrevious()){
            Student s = (Student) lit.previous();
            System.out.println(s.toString());
        }

        //4.判断
        //要实现一下方式删除的前提也是需要重写equals方法
        System.out.println(arrayList.contains(new Student("梁朝伟",24)));
        System.out.println(arrayList.isEmpty());

        //查找元素位置
        System.out.println(arrayList.indexOf(new Student("梁朝伟",24)));

    }
}

输出结果:

相关推荐
折哥的程序人生 · 物流技术专研9 小时前
第8篇:六大设计原则在工厂模式中的体现
java·设计模式·设计原则·工厂模式·编程进阶·扩充系列
问君能有几多愁~9 小时前
C++ 数据结构复习笔记
数据结构·c++·笔记
至乐活着10 小时前
深入解析跳表SkipList:原理、实现与性能优化实战
数据结构·算法·跳表·skiplist·java实现
小明bishe1810 小时前
计算机毕业设计之基于JAVA的植物科普网站
java·spring boot·spring·架构·课程设计
Drone_xjw10 小时前
从 GDB 到 CDB:C/C++ 程序调试的两把“手术刀”
c语言·开发语言·c++
r_oo_ki_e_10 小时前
Java Map 集合学习笔记
java·笔记·学习
SL-staff10 小时前
智慧园区2000+设备统一管理:JVS-IoT如何降低运维成本40%
java·开发语言·物联网·智慧园区·设备管理·运维优化·jvs-iot
咖啡八杯11 小时前
GoF设计模式——模板方法模式
java·后端·spring·设计模式
爱笑的源码基地12 小时前
一款全开源的信创云PACS影像云平台解决方案,支持多模态医学影像处理(CT/MR/DR/超声/病理等)
java·源码·国产化·pacs·云影像·区域pacs
我命由我1234512 小时前
Android 开发问题:ClickableSpan 的点击事件没有生效
java·java-ee·android studio·android jetpack·android-studio·android runtime