集合框架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));
    }
}

代码运行结果:

相关推荐
Cao1234567893213 分钟前
简易学生成绩管理系统(C语言)
c语言·开发语言
The Future is mine5 分钟前
C# new Bitmap(32043, 32043, PixelFormat.Format32bppArgb)报错:参数无效,如何将图像分块化处理?
开发语言·c#
亿坊电商7 分钟前
PHP框架在微服务迁移中能发挥什么作用?
开发语言·微服务·php
烁3477 分钟前
每日一题(小白)模拟娱乐篇33
java·开发语言·算法
坐吃山猪26 分钟前
Python-Agent调用多个Server-FastAPI版本
开发语言·python·fastapi
88号技师28 分钟前
【1区SCI】Fusion entropy融合熵,多尺度,复合多尺度、时移多尺度、层次 + 故障识别、诊断-matlab代码
开发语言·机器学习·matlab·时序分析·故障诊断·信息熵·特征提取
北漂老男孩43 分钟前
Java对象转换的多种实现方式
java·开发语言
小贾要学习1 小时前
【C++】继承----下篇
android·java·c++
未来可期LJ1 小时前
【Test】单例模式❗
开发语言·c++
Arenaschi1 小时前
SQLite 是什么?
开发语言·网络·python·网络协议·tcp/ip