再看看ArrayList

public class ArrayListDemo {
    public static void main(String[] args) {
        int size = 2000000;
        List<String> list = new ArrayList<>();
        for(int i = 0;i<size;i++) {
            list.add("Just some test data");
        }

        long startTime = System.currentTimeMillis();
        for(int i = 0;i<size;i++) {
            list.get(i);
            if(i%10000 ==0) {
                System.out.println("query 10000 elements spend: "+ (System.currentTimeMillis()-startTime));
                startTime = System.currentTimeMillis();
            }
        }
    }
}

程序运行结果:

query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 10
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0
query 10000 elements spend: 0

......和LinkedListWorngDemo对比可以充分说明一个问题
时间: 2024-10-12 17:32:17

再看看ArrayList的相关文章

ArrayList排序

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Collections; 6 7 namespace ArrySort 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 ArrayList arry = new ArrayList() { ne

Vector,ArrayList,LinkedList,SynchronizedList

总体来说,Vector和ArrayList是一致的,实现的接口也是完全一样,内部的存储结构也基本一致[都是数组],每个方法的实现也几乎相同. 区别在于,Vector是线程安全的,每个方法都加了synchronized修饰符,而ArrayList没有. 而且Vector是JDK1.0引入的,而ArrayList是JDK1.2引入的. ==================================强大的分割线====================================== 再来说A

ArrayList 和 LinkList 的区别

ArrayList 的相关知识 public class ArrayList<E> extends AbstractList<E>implements List<E>, RandomAccess, Cloneable, java.io.Serializable 由上面源码可知,Arraylist继承自AbstractList 实现了List ,Cloneable,Serializable,RandomAccess接口.其中Cloneable是克隆接口,Serializa

ArrayList的实现细节(基于JDK1.8)

ArrayList是我们经常用到的一个类,下面总结一下它内部的实现细节和使用时要注意的地方. 基本概念 ArrayList在数据结构的层面上讲,是一个用数组实现的list,从应用层面上讲,就是一个容量会自己改变的数组,具有一系列方便的add.set.get.remove等方法,线程不安全.先上张类图吧. ArrayList的容量 ArrayList有两个数据域与之相关. 1 transient Object[] elementData; // non-private to simplify ne

关于ArrayList.clear()与=null以及new ArrayList&lt;E&gt;()

ArrayList是常用到的JCF类,用来保存类型相同的一组对象,并通过下标来快速访问指定对象.今天讨论的是当我们使用完ArrayList后应该选择怎样合适的处理方式. 这里现在有三种方式如下: 1. 使用ArrayList的对象方法clear() ArrayList<String> list = new ArrayList<String>(); list.add("0"); list.add("1"); list.add("2&q

常用集合ArrayList浅度解析

首先,先看一下java中对ArrayList的定义代码: public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable { private static final long serialVersionUID = 8683452581122892189L; /** * Default initial c

java容器类分析:Collection,List,ArrayList

1. Iterable 与 Iterator Iterable 是个接口,实现此接口使集合对象可以通过迭代器遍历自身元素. public interface Iterable<T> 修饰符和返回值 方法名 描述 Iterator<T> iterator() 返回一个内部元素为T类型的迭代器 default void forEach(Consumer<? super T> action) 对内部元素进行遍历,并对元素进行指定的操作 default Spliterator&

JDK1.8源码(五)——java.util.ArrayList 类

关于 JDK 的集合类的整体介绍可以看这张图,本篇博客我们不系统的介绍整个集合的构造,重点是介绍 ArrayList 类是如何实现的. 1.ArrayList 定义 ArrayList 是一个用数组实现的集合,支持随机访问,元素有序且可以重复. public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializabl

ArrayList.clear、=null、new Arraylist之间的对比区别

参考博文 1.使用ArrayList的对象方法clear() 1 List list = new ArrayList(); 2 List list1 = list; 3 list.add(1); 4 list.add(2); 5 list.add(3); 6 list.clear(); ArrayList对象内部维护一个对象数组elementData, 当执行clear()方法时, 会执行循环将elementData中每一个坐标都设置为为null, 并设置数组的size为0:我们还可以使用该li