zoukankan      html  css  js  c++  java
  • Java自学-集合框架 ArrayList和LinkedList的区别

    ArrayList和LinkedList的区别

    步骤 1 : ArrayList和LinkedList的区别

    ArrayList ,插入,删除数据慢
    LinkedList, 插入,删除数据快
    ArrayList是顺序结构,所以定位很快,指哪找哪。 就像电影院位置一样,有了电影票,一下就找到位置了。
    LinkedList 是链表结构,就像手里的一串佛珠,要找出第99个佛珠,必须得一个一个的数过去,所以定位慢

    ArrayList和LinkedList的区别
    步骤 2 : 插入数据

    插入数据

    package collection;
     
    import java.util.ArrayList;
    import java.util.LinkedList;
    import java.util.List;
     
    public class TestCollection {
        public static void main(String[] args) {
            List<Integer> l;
            l = new ArrayList<>();
            insertFirst(l, "ArrayList");
     
            l = new LinkedList<>();
            insertFirst(l, "LinkedList");
     
        }
     
        private static void insertFirst(List<Integer> l, String type) {
            int total = 1000 * 100;
            final int number = 5;
            long start = System.currentTimeMillis();
            for (int i = 0; i < total; i++) {
                l.add(0, number);
            }
            long end = System.currentTimeMillis();
            System.out.printf("在%s 最前面插入%d条数据,总共耗时 %d 毫秒 %n", type, total, end - start);
        }
     
    }
    

    步骤 3 : 定位数据

    定位数据

    package collection;
     
    import java.util.ArrayList;
    import java.util.LinkedList;
    import java.util.List;
     
    public class TestCollection {
        public static void main(String[] args) {
            List<Integer> l;
            l = new ArrayList<>();
            modify(l, "ArrayList");
     
            l = new LinkedList<>();
            modify(l, "LinkedList");
     
        }
     
        private static void modify(List<Integer> l, String type) {
            int total = 100 * 1000;
            int index = total/2;
            final int number = 5;
            //初始化
            for (int i = 0; i < total; i++) {
                l.add(number);
            }
             
            long start = System.currentTimeMillis();
     
            for (int i = 0; i < total; i++) {
                 int n = l.get(index);
                 n++;
                 l.set(index, n);
            }
            long end = System.currentTimeMillis();
            System.out.printf("%s总长度是%d,定位到第%d个数据,取出来,加1,再放回去%n 重复%d遍,总共耗时 %d 毫秒 %n", type,total, index,total, end - start);
            System.out.println();
        }
     
    }
    

    练习在中间插入数据

    在List的中间位置,插入数据,比较ArrayList快,还是LinkedList快,并解释为什么?

    答案 :

    package collection;
       
    import java.util.ArrayList;
    import java.util.LinkedList;
    import java.util.List;
       
    public class TestCollection {
        public static void main(String[] args) {
            List<Integer> l;
            l = new ArrayList<>();
            insertFirst(l, "ArrayList");
       
            l = new LinkedList<>();
            insertFirst(l, "LinkedList");
       
        }
       
        private static void insertFirst(List<Integer> l, String type) {
            int total = 1000 * 100;
            final int number = 5;
            long start = System.currentTimeMillis();
            for (int i = 0; i < total; i++) {
                //把当前容器的一半的位置,插入数据
                l.add(l.size()/2,number);
            }
            long end = System.currentTimeMillis();
            System.out.printf("在%s 最中间插入%d条数据,总共耗时 %d 毫秒 %n", type, total, end - start);
        }
       
    }
    

    在这个位置插入数据
    数组定位很快,插入数据比较慢
    链表定位很慢,插入数据比较快
    最后发现,当总数是10000条的时候,链表定位的总开支要比数组插入的总开支更多,所以最后整体表现,数组会更好。
    如果总数是1000条,结果可能就不一样了。

  • 相关阅读:
    hdfs fsck命令查看HDFS文件对应的文件块信息(Block)和位置信息(Locations)
    更高的压缩比,更好的性能–使用ORC文件格式优化Hive
    InfluxDB基本概念和操作
    InfluxDB部署
    Zookeeper运维小结--CancelledKeyException
    Zookeeper源码编译为Eclipse工程(win7下Ant编译)
    ZooKeeper Observers解决节点过多时写性能下降问题
    ZooKeeper日志与快照文件简单分析
    ZooKeeper Administrator's Guide A Guide to Deployment and Administration(吃别人嚼过的馍没意思,直接看官网资料)
    ZOOKEEPER解惑
  • 原文地址:https://www.cnblogs.com/jeddzd/p/12112689.html
Copyright © 2011-2022 走看看