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

代码运行结果:

相关推荐
screenCui几秒前
macOS运行python程序遇libiomp5.dylib库冲突错误解决方案
开发语言·python·macos
linux kernel16 分钟前
第七讲:C++中的string类
开发语言·c++
咖啡啡不加糖23 分钟前
RabbitMQ 消息队列:从入门到Spring Boot实战
java·spring boot·rabbitmq
玩代码30 分钟前
Java线程池原理概述
java·开发语言·线程池
NE_STOP33 分钟前
SpringBoot--如何给项目添加配置属性及读取属性
java
水果里面有苹果36 分钟前
20-C#构造函数--虚方法
java·前端·c#
%d%d239 分钟前
python 在运行时没有加载修改后的版本
java·服务器·python
金銀銅鐵1 小时前
[Kotlin] 单例对象是如何实现的?
java·kotlin
泰勒疯狂展开1 小时前
Java研学-MongoDB(三)
java·开发语言·mongodb
zzywxc7871 小时前
AI技术通过提示词工程(Prompt Engineering)正在深度重塑职场生态和行业格局,这种变革不仅体现在效率提升,更在重构人机协作模式。
java·大数据·开发语言·人工智能·spring·重构·prompt