Java容器类分析之List ArrayList Vector

开发 后端
List是接口,声明了各个方法,不多说。且看ArrayList类。ArrayList类的成员变量有Object[] elementData,int size;其中elementData数组用来存储加入到ArrayList的对象,size为列表中实际的对象数目。ArrayList类不是线程安全的。

List是接口,声明了各个方法,不多说。且看ArrayList类。

ArrayList类的成员变量有Object[] elementData,int size;其中elementData数组用来存储加入到ArrayList的对象,size为列表中实际的对象数目。ArrayList类不是线程安全的。

Vector与ArrayList的实现基本相同,只是Vector类是线程安全的,其方法都带有synchronized关键字,如果不考虑线程同步的话,ArrayList性能要好一些。当前它们内部实现原理都是用到对象数组来实现,如果元素数目确定,直接用数组效率***。

 

简单的用法:(后面是数据打印结果)

  1. public class ListDemo {  
  2.  
  3.     /**  
  4.      * @param args  
  5.      */ 
  6.     public static void main(String[] args) {  
  7.         List<String> list = new ArrayList<String>();  
  8.         String[] strArr = new String[3];  
  9.         boolean ret = list.add("haha");  
  10.         list.add(new String("aa"));  
  11.         list.add(null);   
  12.         System.out.println(list.size());//3  
  13.         System.out.println(ret);//true  
  14.         System.out.println(list);//[haha, aa, null]  
  15.         System.out.println(strArr);//[Ljava.lang.String;@1fee6fc  
  16.         System.out.println(strArr.getClass().getName());//[Ljava.lang.String;  
  17.         System.out.println(list.indexOf("aa"));//1  
  18.         System.out.println(list.indexOf(null));//2  
  19.         String str = list.set(1"ee");  
  20.         System.out.println(str);//aa  
  21.         System.out.println(list);//[haha, ee, null]  
  22.         String remove = list.remove(0);  
  23.         System.out.println(remove);//haha  
  24.         System.out.println(list);//[ee, null]  
  25.         boolean result = list.remove("ff");  
  26.         System.out.println(result);//false  
  27.         result = list.remove("ee");  
  28.         System.out.println(result);//true  
  29.         System.out.println(list);//[null]  
  30.     }  
  31.  
  1. public ArrayList() {  
  2.     this(10);  
  3.     }  
  4.  public ArrayList(int initialCapacity) {  
  5.     super();  
  6.             if (initialCapacity < 0)  
  7.                  throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity);  
  8.     this.elementData = new Object[initialCapacity];  
  9.     }  
  10.    public boolean add(E e) {  
  11.     ensureCapacity(size + 1);  // Increments modCount!!  
  12.     elementData[size++] = e;  
  13.     return true;  
  14.     }  
  15.  
  16.  
  17.  
  18. /*移除指定位置元素,注意每次移除数据都会将数组中后面数据移动来填充数组*/ 
  19.  public E remove(int index) {  
  20.     RangeCheck(index);  
  21.  
  22.     modCount++;  
  23.     E oldValue = (E) elementData[index];  
  24.  
  25.     int numMoved = size - index - 1;  
  26.     if (numMoved > 0)  
  27.         System.arraycopy(elementData, index+1, elementData, index,  
  28.                  numMoved);  
  29.     elementData[--size] = null// index后面数据依次往前移动,将***一个位置赋值为0,让gc来回收空间。  
  30.     return oldValue;  
  31.     }  
  32.  
  33. public void ensureCapacity(int minCapacity) {  
  34.     modCount++;//这个变量不用管。  
  35.     int oldCapacity = elementData.length; //初始时设定的数组长度  
  36.     if (minCapacity > oldCapacity) {    //如果数组对象数目>初始数组长度,则需要扩容。  
  37.         Object oldData[] = elementData;  
  38.         int newCapacity = (oldCapacity * 3)/2 + 1//新的容量大小  
  39.             if (newCapacity < minCapacity)  
  40.         newCapacity = minCapacity;  
  41.      /*该方法会创建一个新的对象数组,然后调用  System.arraycopy(original, 0, copy, 0,  
  42.                 Math.min(original.length, newLength));方法将源数组数据拷贝到新数组中。引用更新,指    向新的对象数组。*/ 
  43.                    elementData = Arrays.copyOf(elementData, newCapacity);   
  44.     }  
  45.     }  
  46.  
  47. /*将对象数组削减到当前元素数目大小,减少存储空间*/     
  48. public void trimToSize() {   
  49.     modCount++;  
  50.     int oldCapacity = elementData.length;  
  51.     if (size < oldCapacity) {  
  52.             elementData = Arrays.copyOf(elementData, size);  
  53.     }  
  54.     }  
  55.  
  56. /*查找对象***出现的位置,若没有找到,返回-1。由  
  57. 代码可知,可以在list中加入null对象,并查找到。*/ 
  58.  public int indexOf(Object o) {  
  59.     if (o == null) {  
  60.         for (int i = 0; i < size; i++)  
  61.         if (elementData[i]==null)  
  62.             return i;  
  63.     } else {  
  64.         for (int i = 0; i < size; i++)  
  65.         if (o.equals(elementData[i]))  
  66.             return i;  
  67.     }  
  68.     return -1;  
  69.     }  
  70.  
  71. /*替换指定位置的元素值,返回该位置中old值*/ 
  72. public E set(int index, E element) {  
  73.     RangeCheck(index); //检查范围  
  74.     E oldValue = (E) elementData[index];  
  75.     elementData[index] = element;  
  76.     return oldValue;  
  77.     }  
  78.  
  79. /*返回指定位置的值*/ 
  80. public E get(int index) {  
  81.     RangeCheck(index);  
  82.  
  83.     return (E) elementData[index];  
  84.     }  
  85.  
  86.  private void RangeCheck(int index) {  
  87.     if (index >= size)  
  88.         throw new IndexOutOfBoundsException(  
  89.         "Index: "+index+", Size: "+size);  
  90.     }  
  91.   public int size() {  
  92.     return size;  
  93.     }  
  94.  
  95.     public boolean isEmpty() {  
  96.     return size == 0;  
  97.     } 

原文链接:http://qiemengdao.iteye.com/blog/1415761

【编辑推荐】

  1. 有可能挑战Java优势的四种技术
  2. Think in Java之斐波那契数列
  3. Java的poi技术读取和导入Excel
  4. Java SE 6生命将在今年11月终结
  5. Java中线程安全问题个人理解
责任编辑:林师授 来源: qiemengdao的博客
相关推荐

2011-07-13 14:49:31

STLC++

2019-11-06 16:21:25

ArrayListLinkedListVector

2018-09-29 15:34:34

JavaList接口

2021-04-08 10:10:46

JavaSimpleDateFList接口

2022-09-04 18:00:11

ArrayListVector

2011-07-13 14:58:53

STL容器

2012-03-19 09:57:09

JavaArrayList

2021-12-08 09:11:41

前端

2020-12-14 08:03:52

ArrayList面试源码

2020-07-08 07:56:08

Java工具类包装类

2022-10-26 09:57:52

VectorRustC++

2009-07-08 13:22:30

JDK源码分析Set

2011-06-21 09:22:53

Random类

2011-07-13 15:07:48

STLC++

2021-07-12 11:01:15

Vector元素方法

2021-07-22 09:53:34

Vector类Java添加元素

2021-04-05 08:11:04

Java基础Calendar类DateFormat类

2009-07-22 09:31:59

Scala类类层级Java类

2023-07-13 08:26:49

Java罗汉增强类

2022-06-09 07:27:14

JavaSpring容器
点赞
收藏

51CTO技术栈公众号