java中的List,ArrayList和LinkedList集合

List集合:

void add(int index, E element)

Inserts the specified element at the specified position in this list (optional operation).

在此集合中的指定位置插入指定元素

E remove(int index)

Removes the element at the specified position in this list (optional operation).

删除指定索引处的元素,并返回被删除的元素

E set(int index, E element)

Replaces the element at the specified position in this list with the specified element (optional operation).

修改指定索引处的元素 ,修改成功返回原来的数据

E get(int index)

Returns the element at the specified position in this list.

返回指定索引处的元素

java 复制代码
public class java {
    public static void main(String[] args) {
        List<String>list=new ArrayList<>();//List是一个抽象类
        //List 有序,可重复,有索引

        //add
        list.add("java1");
        list.add("java2");
        list.add("java3");
        System.out.println(list);

        list.add(1,"java4");
        System.out.println(list);//[java1, java4, java2, java3]

        //remove
        System.out.println(list.remove(1));//java4

        //get
        System.out.println(list.get(0));

        //set,返回被修改的数据
        System.out.println(list.set(0, "java20"));//java1
        System.out.println(list);//[java20, java2, java3]
    }
}

List集合的遍历:

1 for循环(List集合有索引)

2 迭代器

3 for循环增强

4 Lambda表达式

java 复制代码
public class test {
    public static void main(String[] args) {
        List<String>list=new ArrayList<>();
        list.add("java1");
        list.add("java2");
        list.add("java3");
        System.out.println(list);
        //for循环
        for(int i=0;i<list.size();i++)
        {
            System.out.println(list.get(i));
        }

        //迭代器
        Iterator<String> iterator = list.iterator();
        while(iterator.hasNext())
        {
            String next = iterator.next();
            System.out.println(next);
        }

        //增强for循环
        for(String s:list)
        {
            System.out.println(s);
        }
        //Lambda表达式
        list.forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        });
        list.forEach(s-> System.out.println(s));
        list.forEach(System.out::println);
    }
}

ArrayList集合的底层原理

基于数组实现的

特点:

查询数据快:(注意:是根据索引查询数据块)

删除效率低:可能需要把后面的大量数据往前移动

添加效率低:将后面大量数据后移动

ArrayList适合根据随机索引取数据(高效)或者数据不是很大的时候

不适合数据量大很大,又频繁的增删数据

LinkedList集合的底层逻辑

基于双向链表实现的

提点:

查询满,增删块**,但是对首尾元素的增删改查的速度是极快的**

public void addFirst(E e)

Inserts the specified element at the beginning of this list.

public void addLast(E e)

Appends the specified element to the end of this list.

public E getFirst()

Returns the first element in this list.

public E getLast()

Returns the last element in this list.

public E removeFirst()

Removes and returns the first element from this list.

从链表中返回第一个元素,并且删除

public E removeLast()

Removes and returns the last element from this list.

用于创建队列

java 复制代码
public class test {
    public static void main(String[] args) {
        LinkedList <String>queue=new LinkedList<>();

        //入队
        queue.addLast("java1");
        queue.addLast("java2");

        //出队
        System.out.println(queue.removeFirst());//java1
        System.out.println(queue.removeFirst());//java2
    }
}

用于创建栈

java 复制代码
public class test2 {
    public static void main(String[] args) {
        LinkedList<String>stack=new LinkedList<>();

        //入栈
        /*stack.addFirst("java1");
        stack.addFirst("java2");*/
        stack.push("java1");
        stack.push("java2");

        //出栈
       /* System.out.println(stack.removeFirst());//java2
        System.out.println(stack.removeFirst());//java1*/
        System.out.println(stack.pop());//java2
        System.out.println(stack.pop());//java1
    }
}
相关推荐
卷毛的技术笔记14 分钟前
告别硬编码!Spring AI Alibaba 实现 AI Agent 智能工具调用(Tool Calling)
java·人工智能·后端·python·spring·ai编程
编程大师哥14 分钟前
匿名函数 lambda + 高阶函数
java·python·算法
isyangli_blog16 分钟前
OpenDayLight (Carbon 版本) 启动与组件安装
开发语言·php
vb20081124 分钟前
FastAPI APIRouter
开发语言·python
Benszen26 分钟前
KVM虚拟化解决方案
开发语言·perl
会编程的土豆27 分钟前
Go 语言反射(Reflection)详解
开发语言·后端·golang
東雪木29 分钟前
多线程与并发编程 专属复习笔记
java·开发语言·笔记·java面试
adrninistrat0r34 分钟前
Java调用链MCP分析工具
java·python·ai编程
杨充1 小时前
1.3 浮点型数据设计灵魂
开发语言·python·算法
噜噜噜阿鲁~1 小时前
python学习笔记 | 11.3、面向对象高级编程-多重继承
java·开发语言