zoukankan      html  css  js  c++  java
  • Java基础——ArrayList与LinkedList(一)

    一、定义

    ArrayList和LinkedList是两个集合类,用于储存一系列的对象引用(references)。

    引用的格式分别为:

     1 ArrayList<String> list = new ArrayList<String>(); 

     1 LinkedList<Integer> list = new LinkedList<Integer>(); 

    二、ArrayList与LinkedList的大致区别

    1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构;

    2.对于随机访问get和set,ArrayList优先于LinkedList。因为LinkedList要移动指针。

    3.对于新增和删除操作(add和remove),LinkedList比较占优势,因为ArrayList要移动数据。

    (关于3,网上又争论,意思是说对大型数据的增删,收尾增删或对中间某位作用,效果是不同的,有待考证!)

    三、ArrayList与LinkedList常用方法

    代码实例:

     1 import java.util.ArrayList;
     2 
     3 public class CollT2 {
     4 
     5     public static void main(String[] args) {
     6 
     7         // 新建一个ArrayList
     8         ArrayList<String> list = new ArrayList<String>();
     9         System.out.println("初始化大小:" + list.size());
    10 
    11         // 添加元素
    12         list.add("北京");
    13         list.add("天津");
    14         list.add("上海");
    15         list.add("广州");
    16         list.add("深圳");
    17         list.add("海口");
    18         list.add("厦门");
    19         System.out.println("当前容量:" + list.size());
    20 
    21         // 讲ArrayList的大小和实际所含元素的大小设置一致
    22         // 注意:此操作演示下面会抛出的异常:java.util.ConcurrentModificationException
    23         list.trimToSize();
    24 
    25         // 遍历
    26         for (String string : list) {
    27             System.out.println(string);
    28 
    29             // 在指定位置插入元素
    30             list.add(2, "黑龙江");
    31             for (String string1 : list) {
    32                 System.out.println(string1);
    33             }
    34             System.out.println("=======分割线========");
    35 
    36             // 清空list
    37             list.clear();
    38             for (String string3 : list) {
    39                 System.out.println(string3);
    40             }
    41 
    42         }
    43     }
    44 }

      1 import java.util.LinkedList;
      2 
      3 //ArrayList基于数组实现,所以它具备数组的特点,即查询速度较快,但是修改、插入的速度却有点儿慢。
      4 //下面将要介绍的LinkedList就是来解决这个问题的,LinkedList基于链表,与ArrayList互补。
      5 //所以实际开发中我们应该按照自己的需求来定到底用哪一个。
      6 
      7 //LinkedList底层采用双向循环列表实现,进行插入和删除操作时具有较高的速度.
      8 //我们还可以使用LinkedList来实现队列和栈
      9 
     10 public class CollT3 {
     11     @SuppressWarnings("null")
     12     public static void main(String[] args) {
     13 
     14         // 創建一個list
     15         LinkedList<Integer> list = new LinkedList<Integer>();
     16         LinkedList<Integer> list2 = new LinkedList<Integer>();
     17         LinkedList<Integer> list3 = new LinkedList<Integer>();
     18         LinkedList<Integer> list4 = new LinkedList<Integer>();
     19         
     20         System.out.println(list.size());
     21 
     22         // 添加元素
     23         // list.add("锅包肉");没考虑数据类型
     24         // list.add("溜肉段");
     25         System.out.println("====分割线1====");
     26 
     27         list.add(5);
     28         list.add(6);
     29         list.add(7);
     30         list.add(8);
     31         list.add(9);
     32         list.add(10);
     33 
     34         list2.add(-1);
     35         list2.add(-2);
     36         list2.add(-3);
     37         list2.add(-4);
     38         list2.add(-5);
     39 
     40         list3.add(111);
     41         list3.add(222);
     42         list3.add(333);
     43         list3.add(444);
     44         list3.add(555);
     45         
     46         list4=null;
     47 
     48         System.out.println(list.size());
     49 
     50         // 遍历
     51         for (Integer a : list) {
     52             System.out.println(a);
     53         }
     54 
     55         list.add(4, 11111);
     56         System.out.println(list);// 竟然是横着打印出来的
     57         System.out.println("===分割线2====");
     58 
     59         list.add(3, 22222);
     60         System.out.println(list);// 二次验证竟然是横着打印出来的
     61 
     62         System.out.println("===分割线3====");
     63         System.out.println(list.addAll(list2));
     64         System.out.println(list);
     65 
     66         System.out.println("===分割线4====");
     67         // 错误:System.out.println(2,list.addAll(list3));
     68         list.addAll(2, list3);// 将指定 collection 中的所有元素从指定位置开始插入此列表。
     69         System.out.println(list);
     70 
     71         System.out.println("===分割线5====");
     72         list3.addFirst(3);//需要单独打印
     73         System.out.println(list3);
     74         
     75         System.out.println("===分割线6====");
     76         list3.addLast(0000000001);//1
     77         list3.addLast(000000000);//0
     78         list3.addLast(0);//0
     79         //list3.addLast(00000000000000009);//The literal 00000000000000009 of type int is out of range 
     80         list3.addLast(0000000000000000000000000000000000007);//末尾只能写到7,8和9不行
     81         System.out.println(list3);
     82         
     83         list.clear();
     84         System.out.println(list);//[]
     85         
     86         System.out.println("===分割线7====");
     87         
     88         list2.clone();//返回此 LinkedList的浅表副本。
     89         System.out.println(list);
     90         
     91         System.out.println("===分割线8====");
     92         //list3.contains(999999999);//呃,竟然不是这么验证的!!!!
     93         System.out.println(list3.contains(999999999));// 如果此列表包含指定元素,则返回 true。
     94         
     95         System.out.println("===分割线9====");
     96         //list3.element();//呃,竟然不是这么验证的!!!!
     97         System.out.println(list3.element());//获取但不移除此列表的头(第一个元素)。
     98         System.out.println(list3);//验证上面的操作真的只是获取没有移除
     99         
    100         System.out.println("===分割线10====");//注意上面对list3的操作,给它头位添加个3了
    101         System.out.println(list3.get(2));// 返回此列表中指定位置处的元素。
    102         
    103         System.out.println("===分割线11====");
    104         System.out.println(list3.getFirst());//返回此列表的第一个元素。
    105         System.out.println(list3.getLast());//返回此列表的最后一个元素。
    106         
    107         System.out.println("===分割线12====");
    108         System.out.println(list3.indexOf(333));//返回此列表中首次出现的指定元素的索引,如果此列表中不包含该元素,则返回 -1。
    109         System.out.println(list3.indexOf(10));
    110         
    111         System.out.println("===分割线13====");
    112         list3.add(333);
    113         list3.add(333);
    114         list3.add(333);
    115         System.out.println(list3);
    116         System.out.println(list3.lastIndexOf(0));//返回此列表中最后出现的指定元素的索引,如果此列表中不包含该元素,则返回 -1。
    117         System.out.println(list3.lastIndexOf(9));//-1
    118         
    119         System.out.println("===分割线14====");
    120         list3.offer(6);//将指定元素添加到此列表的末尾(最后一个元素)。
    121         System.out.println(list3.offer(6));//true
    122         System.out.println(list3);
    123         
    124         System.out.println("===分割线15====");
    125         System.out.println(list3.peek());// 获取但不移除此列表的头(第一个元素)。
    126         
    127         //peekFirst();获取但不移除此列表的最后一个元素;如果此列表为空,则返回 null。
    128         //System.out.println(list4.peekFirst());上面list4定义为null,黄色报错。用了@SuppressWarnings("null")。验证:java.lang.NullPointerException
    129         //System.out.println(list4.peekLast());java.lang.NullPointerException
    130         
    131         System.out.println("===分割线16====");
    132         System.out.println(list3);
    133         list3.poll();
    134         System.out.println(list3);
    135         
    136         System.out.println("===分割线17====");
    137         System.out.println(list3);
    138         list3.toArray();//返回以适当顺序(从第一个元素到最后一个元素)包含此列表中所有元素的数组。
    139         System.out.println(list3);
    140     }
    141 
    142 }

    打印结果:

    0
    ====分割线1====
    6
    5
    6
    7
    8
    9
    10
    [5, 6, 7, 8, 11111, 9, 10]
    ===分割线2====
    [5, 6, 7, 22222, 8, 11111, 9, 10]
    ===分割线3====
    true
    [5, 6, 7, 22222, 8, 11111, 9, 10, -1, -2, -3, -4, -5]
    ===分割线4====
    [5, 6, 111, 222, 333, 444, 555, 7, 22222, 8, 11111, 9, 10, -1, -2, -3, -4, -5]
    ===分割线5====
    [3, 111, 222, 333, 444, 555]
    ===分割线6====
    [3, 111, 222, 333, 444, 555, 1, 0, 0, 7]
    []
    ===分割线7====
    []
    ===分割线8====
    false
    ===分割线9====
    3
    [3, 111, 222, 333, 444, 555, 1, 0, 0, 7]
    ===分割线10====
    222
    ===分割线11====
    3
    7
    ===分割线12====
    3
    -1
    ===分割线13====
    [3, 111, 222, 333, 444, 555, 1, 0, 0, 7, 333, 333, 333]
    8
    -1
    ===分割线14====
    true
    [3, 111, 222, 333, 444, 555, 1, 0, 0, 7, 333, 333, 333, 6, 6]
    ===分割线15====
    3
    ===分割线16====
    [3, 111, 222, 333, 444, 555, 1, 0, 0, 7, 333, 333, 333, 6, 6]
    [111, 222, 333, 444, 555, 1, 0, 0, 7, 333, 333, 333, 6, 6]
    ===分割线17====
    [111, 222, 333, 444, 555, 1, 0, 0, 7, 333, 333, 333, 6, 6]
    [111, 222, 333, 444, 555, 1, 0, 0, 7, 333, 333, 333, 6, 6]
  • 相关阅读:
    2. Add Two Numbers
    1. Two Sum
    leetcode 213. 打家劫舍 II JAVA
    leetcode 48. 旋转图像 java
    leetcode 45. 跳跃游戏 II JAVA
    leetcode 42. 接雨水 JAVA
    40. 组合总和 II leetcode JAVA
    24. 两两交换链表中的节点 leetcode
    1002. 查找常用字符 leecode
    leetcode 23. 合并K个排序链表 JAVA
  • 原文地址:https://www.cnblogs.com/1693977889zz/p/7073900.html
Copyright © 2011-2022 走看看