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

代码运行结果:

相关推荐
与遨游于天地几秒前
Spring解决循环依赖实际就是用了个递归
java·后端·spring
陈果然DeepVersion3 分钟前
Java大厂面试真题:Spring Boot+微服务+AI智能客服三轮技术拷问实录(六)
java·spring boot·redis·微服务·面试题·rag·ai智能客服
星星火柴93618 分钟前
笔记 | C++面向对象高级开发
开发语言·c++·笔记·学习
码界奇点25 分钟前
Rust 性能优化全流程从 flamegraph 定位瓶颈到 unsafe 与 SIMD 加速响应快
开发语言·性能优化·rust·simulated annealing
BeingACoder27 分钟前
【SAA】SpringAI Alibaba学习笔记(一):SSE与WS的区别以及如何注入多个AI模型
java·笔记·学习·saa·springai
DolphinScheduler社区28 分钟前
真实迁移案例:从 Azkaban 到 DolphinScheduler 的选型与实践
java·大数据·开源·任务调度·azkaban·海豚调度·迁移案例
丛雨要玩游戏1 小时前
字符函数和字符串函数
c语言·开发语言·算法
zhangkaixuan4561 小时前
Apache Paimon 写入流程
java·大数据·apache·paimon
八个程序员1 小时前
自定义函数(C++)
开发语言·c++·算法
ad钙奶长高高1 小时前
【C语言】初始C语言
c语言·开发语言·算法