集合框架07:LinkedList使用

1.视频链接:13.14 LinkedList使用_哔哩哔哩_bilibilihttps://www.bilibili.com/video/BV1zD4y1Q7Fw?spm_id_from=333.788.videopod.episodes&vd_source=b5775c3a4ea16a5306db9c7c1c1486b5&p=142.LinkedList集合的增删改查操作

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

import javax.swing.*;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;

public class LinkedListDemo01 {
    public static void main(String[] args) {
        //创建集合
        LinkedList linkedList = new LinkedList<>();

        //1.添加元素
        Student s1 = new Student("刘德华", 30);
        Student s2 = new Student("张学友", 30);
        Student s3 = new Student("郭富城", 30);
        Student s4 = new Student("梁朝伟", 30);
        linkedList.add(s1);
        linkedList.add(s2);
        linkedList.add(s3);
        linkedList.add(s4);

        //2.删除元素
//        linkedList.remove(s1);
//        linkedList.remove( new Student("张学友", 30));
        System.out.println("删除后元素个数" + linkedList.size());
        System.out.println(linkedList.toString());


        //3.遍历元素
        //3.1使用for循环
        System.out.println("-------使用for循环---------");
        for (int i = 0; i < linkedList.size(); i++) {
            System.out.println((Student)linkedList.get(i));
        }
        //3.2使用增强for循环
        System.out.println("-------使用for循环---------");
        for (Object object : linkedList) {
            Student s = (Student) object;
            System.out.println(s.toString());
        }
        //3.2使用iterator迭代器
        System.out.println("-------使用iterator迭代器---------");
        Iterator iterator = linkedList.iterator();
        while (iterator.hasNext()){
            Student s = (Student) iterator.next();
            System.out.println(s.toString());
        }

        //3.2使用listIterator迭代器
        System.out.println("-------使用iterator迭代器---------");
        ListIterator listIterator = linkedList.listIterator();
        while (listIterator.hasPrevious()){
            Student s = (Student) listIterator.next();
            System.out.println(s.toString());
        }


        //4.判断元素是否在集合中
        System.out.println(linkedList.contains(s3));
        System.out.println(linkedList.isEmpty());


        //5.获取元素在集合中的位置
        System.out.println(linkedList.indexOf(s4));
    }
}

代码运行结果:

相关推荐
weixin_46244623几秒前
PaddleX 3.2 人脸识别实战:自定义人脸库 + CartoonFace 官方案例 Top-K 识别完整指南
开发语言·r语言
Testopia17 分钟前
走一遍 AI 学习之路 —— AI实例系列说明
开发语言·人工智能·python
码农娟17 分钟前
Hutool XML工具-XmlUtil的使用
xml·java
Tony Bai19 分钟前
【分布式系统】11 理论的试金石:用 Go 从零实现一个迷你 Raft 共识
开发语言·后端·golang
Beginner x_u20 分钟前
JavaScript 原型、原型链与原型继承的核心机制解析
开发语言·javascript·原型模式·原型原型链
离离茶20 分钟前
【笔记1-11】Qt 关闭QToolbar的拓展菜单
开发语言·笔记·qt
曹牧22 分钟前
C#:窗体构造函数无法引用窗体控件
开发语言·c#
草青工作室24 分钟前
java-FreeMarker3.4自定义异常处理
java·前端·python
xb113225 分钟前
C#使用Cancellation来取消异步任务
开发语言·c#
m0_7482299927 分钟前
C与C#:编程语言的核心差异解析
c语言·开发语言·c#