自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Java容器類分析之List ArrayList Vector

開發(fā) 后端
List是接口,聲明了各個方法,不多說。且看ArrayList類。ArrayList類的成員變量有Object[] elementData,int size;其中elementData數(shù)組用來存儲加入到ArrayList的對象,size為列表中實際的對象數(shù)目。ArrayList類不是線程安全的。

List是接口,聲明了各個方法,不多說。且看ArrayList類。

ArrayList類的成員變量有Object[] elementData,int size;其中elementData數(shù)組用來存儲加入到ArrayList的對象,size為列表中實際的對象數(shù)目。ArrayList類不是線程安全的。

Vector與ArrayList的實現(xiàn)基本相同,只是Vector類是線程安全的,其方法都帶有synchronized關(guān)鍵字,如果不考慮線程同步的話,ArrayList性能要好一些。當(dāng)前它們內(nèi)部實現(xiàn)原理都是用到對象數(shù)組來實現(xiàn),如果元素數(shù)目確定,直接用數(shù)組效率***。

 

簡單的用法:(后面是數(shù)據(jù)打印結(jié)果)

  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. /*移除指定位置元素,注意每次移除數(shù)據(jù)都會將數(shù)組中后面數(shù)據(jù)移動來填充數(shù)組*/ 
  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后面數(shù)據(jù)依次往前移動,將***一個位置賦值為0,讓gc來回收空間。  
  30.     return oldValue;  
  31.     }  
  32.  
  33. public void ensureCapacity(int minCapacity) {  
  34.     modCount++;//這個變量不用管。  
  35.     int oldCapacity = elementData.length; //初始時設(shè)定的數(shù)組長度  
  36.     if (minCapacity > oldCapacity) {    //如果數(shù)組對象數(shù)目>初始數(shù)組長度,則需要擴(kuò)容。  
  37.         Object oldData[] = elementData;  
  38.         int newCapacity = (oldCapacity * 3)/2 + 1//新的容量大小  
  39.             if (newCapacity < minCapacity)  
  40.         newCapacity = minCapacity;  
  41.      /*該方法會創(chuàng)建一個新的對象數(shù)組,然后調(diào)用  System.arraycopy(original, 0, copy, 0,  
  42.                 Math.min(original.length, newLength));方法將源數(shù)組數(shù)據(jù)拷貝到新數(shù)組中。引用更新,指    向新的對象數(shù)組。*/ 
  43.                    elementData = Arrays.copyOf(elementData, newCapacity);   
  44.     }  
  45.     }  
  46.  
  47. /*將對象數(shù)組削減到當(dāng)前元素數(shù)目大小,減少存儲空間*/     
  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. /*查找對象***出現(xiàn)的位置,若沒有找到,返回-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. 有可能挑戰(zhàn)Java優(yōu)勢的四種技術(shù)
  2. Think in Java之斐波那契數(shù)列
  3. Java的poi技術(shù)讀取和導(dǎo)入Excel
  4. Java SE 6生命將在今年11月終結(jié)
  5. Java中線程安全問題個人理解
責(zé)任編輯:林師授 來源: qiemengdao的博客
相關(guān)推薦

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容器

2024-06-03 10:07:22

Vector類元素向量

2021-12-08 09:11:41

前端

2012-03-19 09:57:09

JavaArrayList

2022-10-26 09:57:52

VectorRustC++

2020-12-14 08:03:52

ArrayList面試源碼

2009-07-08 13:22:30

JDK源碼分析Set

2020-07-08 07:56:08

Java工具類包裝類

2025-02-03 09:10:04

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基礎(chǔ)Calendar類DateFormat類

2023-07-13 08:26:49

Java羅漢增強(qiáng)類
點贊
收藏

51CTO技術(shù)棧公眾號