• List类使用


    基本操作

    //声明集合
    List<String> names = List.of("a", "b", "c"); //[a, b, c]
    
    • 1
    • 2

    of等静态方法声明的集合对象是不可修改的(包括添加和删除),如果想要更改集合中元素,可以将集合传递到构造器中。

    //声明
    var names2 = new ArrayList<>(List.of("a", "b", "c"));
    System.out.println(names2);//[a, b, c]
    //修改
    names2.set(2, "d");
    System.out.println(names2);//[a, b, d],修改List元素成功
    //添加
    names2.add(2, "e");//[a, b, e, d]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    Arrays.asList

    返回一个可更改但是大小不可变的列表。即可以在此列表上调用set,但是不能使用add或remove。

    另外,类似方法还有Collections.emptySet和Collections.singleton。

    ArrayList

    简介

    ArrayLIst是一个其容量可以动态增长的动态数组,继承了AbstractList,实现了List、RandomAccess, Cloneable, java.io.Serializable。
    ArrayList的操作不是线性安全的,一般在单线程中使用。多线程中一般使用Vector或者CopyOnWriteArrayList。

    ArrayList遍历方式

    1.迭代器遍历

    Iterator<Integer> it = arrayList.iterator();
    while(it.hasNext()){
        System.out.print(it.next() + " ");
    }
    
    • 1
    • 2
    • 3
    • 4

    2.索引值遍历

    for(int i = 0; i < arrayList.size(); i++){
       System.out.print(arrayList.get(i) + " ");
    }
    
    • 1
    • 2
    • 3

    3.for循环遍历

    for(Integer number : arrayList){
       System.out.print(number + " ");
    }
    
    • 1
    • 2
    • 3

    遍历效率排序:索引值遍历>for循环遍历>迭代器遍历。

    toArray()的使用

    toArray()返回的是Object[]数组,将Object[]转换为其他类型时,会报类转换异常。因此一般使用方法如下:

    <T> T[] toArray(T[] a)
    
    • 1

    示例:

    // toArray用法
     // 第一种方式(最常用)
     Integer[] integer = arrayList.toArray(new Integer[0]);
    
     // 第二种方式(容易理解)
     Integer[] integer1 = new Integer[arrayList.size()];
     arrayList.toArray(integer1);
    
     // 抛出异常,java不支持向下转型
     //Integer[] integer2 = new Integer[arrayList.size()];
     //integer2 = arrayList.toArray();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    LinkedList

    简介

    • LinkedList是一个继承于AbstractSequentialList的双向链表。它也可以被当作堆栈、队列或双端队列进行操作。
    • LinkedList实现List接口,能进行队列操作。
    • LinkedList实现Deque接口,即能将LinkedList当做双端队列使用。
    • ArrayList底层是由数组支持,而LinkedList是由双向链表实现的。

    LinkedList遍历方式

    1.迭代器遍历

    Iterator<Integer> iterator = linkedList.iterator();
    while(iterator.hasNext()){
        iterator.next();
    }
    
    • 1
    • 2
    • 3
    • 4

    2.for循环遍历

    for(int i = 0; i < linkedList.size(); i++){
        linkedList.get(i);
    }
    
    • 1
    • 2
    • 3

    3.Foreach遍历

    for(Integer i : linkedList);
    
    • 1

    4.通过pollFirst()或pollLast()遍历

    while(linkedList.size() != 0){
        linkedList.pollFirst();
    }
    
    • 1
    • 2
    • 3

    5.通过removeFirst()或removeLast()遍历

    while(linkedList.size() != 0){
        linkedList.removeFirst();
    }
    
    • 1
    • 2
    • 3

    注:使用pollFirst()或pollLast()或removeFirst()或removeLast()遍历时,会删除原始数据。

    LinkedList和ArrayList比较

    • LinkedList中插入元素很快,而ArrayList中插入元素很慢
    • LinkedList中随机访问很慢,而ArrayList中随机访问很快
  • 相关阅读:
    计算机毕业设计django基于python研究生备考互助系统
    第一季:1自增变量【Java面试题】
    【数据结构-进阶】二叉搜索树
    Vitalik:探索公共物品资金分配优先次序-Revenue-Evil 曲线
    企业架构LNMP学习笔记56
    shell 重定向
    学术加油站|面向HTAP数据库的基准评测工具研究进展
    Flutter整体框架
    22、Mybatis查询功能3(查询结果为一个map集合(一条数据))
    Codeforces Round #827 (Div. 4)
  • 原文地址:https://blog.csdn.net/qq_24889005/article/details/127351752