Java容器類(lèi)ListArrayListVector有什么作用

本篇內(nèi)容主要講解“Java容器類(lèi)List ArrayList Vector有什么作用”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“Java容器類(lèi)List ArrayList Vector有什么作用”吧!

公司主營(yíng)業(yè)務(wù):做網(wǎng)站、成都網(wǎng)站制作、移動(dòng)網(wǎng)站開(kāi)發(fā)等業(yè)務(wù)。幫助企業(yè)客戶(hù)真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競(jìng)爭(zhēng)能力。創(chuàng)新互聯(lián)公司是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開(kāi)放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來(lái)的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶(hù)帶來(lái)驚喜。創(chuàng)新互聯(lián)公司推出江蘇免費(fèi)做網(wǎng)站回饋大家。

List是接口,聲明了各個(gè)方法,不多說(shuō)。且看ArrayList類(lèi)。

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

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

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

public class ListDemo {       /**       * @param args       */     public static void main(String[] args) {          List<String> list = new ArrayList<String>();          String[] strArr = new String[3];          boolean ret = list.add("haha");          list.add(new String("aa"));          list.add(null);           System.out.println(list.size());//3          System.out.println(ret);//true          System.out.println(list);//[haha, aa, null]          System.out.println(strArr);//[Ljava.lang.String;@1fee6fc          System.out.println(strArr.getClass().getName());//[Ljava.lang.String;          System.out.println(list.indexOf("aa"));//1          System.out.println(list.indexOf(null));//2          String str = list.set(1, "ee");          System.out.println(str);//aa          System.out.println(list);//[haha, ee, null]          String remove = list.remove(0);          System.out.println(remove);//haha          System.out.println(list);//[ee, null]          boolean result = list.remove("ff");          System.out.println(result);//false          result = list.remove("ee");          System.out.println(result);//true          System.out.println(list);//[null]      }   }
public ArrayList() {      this(10);      }   public ArrayList(int initialCapacity) {      super();              if (initialCapacity < 0)                   throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity);      this.elementData = new Object[initialCapacity];      }     public boolean add(E e) {      ensureCapacity(size + 1);  // Increments modCount!!      elementData[size++] = e;      return true;      }     /*移除指定位置元素,注意每次移除數(shù)據(jù)都會(huì)將數(shù)組中后面數(shù)據(jù)移動(dòng)來(lái)填充數(shù)組*/  public E remove(int index) {      RangeCheck(index);       modCount++;      E oldValue = (E) elementData[index];       int numMoved = size - index - 1;      if (numMoved > 0)          System.arraycopy(elementData, index+1, elementData, index,                   numMoved);      elementData[--size] = null; // index后面數(shù)據(jù)依次往前移動(dòng),將***一個(gè)位置賦值為0,讓gc來(lái)回收空間。      return oldValue;      }   public void ensureCapacity(int minCapacity) {      modCount++;//這個(gè)變量不用管。      int oldCapacity = elementData.length; //初始時(shí)設(shè)定的數(shù)組長(zhǎng)度      if (minCapacity > oldCapacity) {    //如果數(shù)組對(duì)象數(shù)目>初始數(shù)組長(zhǎng)度,則需要擴(kuò)容。          Object oldData[] = elementData;          int newCapacity = (oldCapacity * 3)/2 + 1; //新的容量大小              if (newCapacity < minCapacity)          newCapacity = minCapacity;       /*該方法會(huì)創(chuàng)建一個(gè)新的對(duì)象數(shù)組,然后調(diào)用  System.arraycopy(original, 0, copy, 0,                  Math.min(original.length, newLength));方法將源數(shù)組數(shù)據(jù)拷貝到新數(shù)組中。引用更新,指    向新的對(duì)象數(shù)組。*/                    elementData = Arrays.copyOf(elementData, newCapacity);       }      }   /*將對(duì)象數(shù)組削減到當(dāng)前元素?cái)?shù)目大小,減少存儲(chǔ)空間*/     public void trimToSize() {       modCount++;      int oldCapacity = elementData.length;      if (size < oldCapacity) {              elementData = Arrays.copyOf(elementData, size);      }      }   /*查找對(duì)象***出現(xiàn)的位置,若沒(méi)有找到,返回-1。由  代碼可知,可以在list中加入null對(duì)象,并查找到。*/  public int indexOf(Object o) {      if (o == null) {          for (int i = 0; i < size; i++)          if (elementData[i]==null)              return i;      } else {          for (int i = 0; i < size; i++)          if (o.equals(elementData[i]))              return i;      }      return -1;      }   /*替換指定位置的元素值,返回該位置中old值*/ public E set(int index, E element) {      RangeCheck(index); //檢查范圍      E oldValue = (E) elementData[index];      elementData[index] = element;      return oldValue;      }   /*返回指定位置的值*/ public E get(int index) {      RangeCheck(index);       return (E) elementData[index];      }    private void RangeCheck(int index) {      if (index >= size)          throw new IndexOutOfBoundsException(          "Index: "+index+", Size: "+size);      }    public int size() {      return size;      }       public boolean isEmpty() {      return size == 0;      }

到此,相信大家對(duì)“Java容器類(lèi)List ArrayList Vector有什么作用”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢(xún),關(guān)注我們,繼續(xù)學(xué)習(xí)!

本文名稱(chēng):Java容器類(lèi)ListArrayListVector有什么作用
當(dāng)前網(wǎng)址:http://muchs.cn/article38/ihissp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司Google、小程序開(kāi)發(fā)營(yíng)銷(xiāo)型網(wǎng)站建設(shè)動(dòng)態(tài)網(wǎng)站、品牌網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁(yè)設(shè)計(jì)公司