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
    }
}
相关推荐
东东5161 分钟前
OA自动化居家办公管理系统 ssm+vue
java·前端·vue.js·后端·毕业设计·毕设
没有bug.的程序员3 分钟前
Spring Cloud Alibaba:Nacos 配置中心与服务发现的工业级深度实战
java·spring boot·nacos·服务发现·springcloud·配置中心·alibaba
先做个垃圾出来………9 分钟前
SortedList(2)
开发语言
云栖梦泽10 分钟前
易语言开发从入门到精通:补充篇·文件批量操作深度实战·常用格式处理·自动化脚本开发·性能优化
开发语言
Big Cole16 分钟前
PHP面试题(核心基础篇:垃圾回收+自动加载)
android·开发语言·php
最懒的菜鸟16 分钟前
Windows系统安装Hyperf 3.X完整版
windows
rainbow688916 分钟前
Java并发三要素:原子性、可见性、有序性
java
2301_8223754419 分钟前
Python虚拟环境(venv)完全指南:隔离项目依赖
jvm·数据库·python
m0_7066532319 分钟前
跨语言调用C++接口
开发语言·c++·算法
2301_7903009620 分钟前
Python类型提示(Type Hints)详解
jvm·数据库·python