posts - 5, comments - 24, trackbacks - 0, articles - 20
            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          ArrayList的用法

          Posted on 2006-09-24 22:31 kook 閱讀(1950) 評(píng)論(1)  編輯  收藏 所屬分類: J2SE

          他是List接口的實(shí)現(xiàn)類。ArrayList類相當(dāng)于是一個(gè)動(dòng)態(tài)數(shù)組。

          Methods:

          1、? void add Object obj

          ArrayList的對(duì)象里增加一個(gè)元素

          2、? set (int?index, Object ?element)

          用指定的元素替代此列表中指定位置上的元素。

          3、? int size ()

          獲得ArrayList的對(duì)象中元素的個(gè)數(shù)。

          4、? get int index)

          返回ArrayList的對(duì)象中索引為index的元素。

          5、? Object [] toArray ()

          ArrayList的對(duì)象中的元素返回到一個(gè)對(duì)象數(shù)組中。

          PS Arrays.asList(Object[] objs);

          ??? 返回一個(gè)受指定數(shù)組支持的固定大小的列表。

          ?1 ArrayList?al? = ? new ?ArrayList();
          ?2
          ????????
          ?3 ????????al.add( new ?Point( 3 , 3
          ));
          ?4 ????????al.add( new ?Point( 4 , 4
          ));
          ?5 ????????al.add( new ?Point( 5 , 5
          ));
          ?6
          ????????
          ?7 ????????
          /* for(int?i=0;i<al.size();i++)
          ?8
          ????????{
          ?9
          ????????????System.out.println(al.get(i));
          10 ????????} */

          11 ????????
          12
          ????????System.out.println(al);
          13 ????????Object[]?objs? =
          ?al.toArray();
          14
          ????????System.out.println(Arrays.toString(objs));
          15 ????????List?L? =
          ?Arrays.asList(objs);
          16

          ???? 這里的L是通過(guò)Arrays.asList返回一個(gè)接口。這時(shí)候L的長(zhǎng)度就固定不能再變了,不能給L添加元素了。但是可以通過(guò)set方法改變L中指定元素的值。

          ????? ? toArray 方法是將一個(gè)List對(duì)象轉(zhuǎn)成一個(gè)數(shù)組,而Arrays.asList方法是將一個(gè)數(shù)組轉(zhuǎn)成一個(gè)List。他們是集合和數(shù)組之間的橋梁,有時(shí)候方法中的參數(shù)可能需要數(shù)組或者List的時(shí)候,就可以用到他們轉(zhuǎn)換,而不用去重新創(chuàng)建實(shí)例。

          6、? iterator()

          返回一個(gè)迭代器。所有繼承Collection接口的接口或者這些接口的實(shí)現(xiàn)類,都有這個(gè)方法。通過(guò)List接口對(duì)象返回的迭代器沒(méi)有實(shí)現(xiàn)iterator接口中的remove方法。凡是沒(méi)有實(shí)現(xiàn)iterator接口中的remove方法,都會(huì)拋出一個(gè) UnsupportedOperationException (不支持的操作)異常。如:

          ?

          1 List?l? = ? null ;??????????????? // List?接口對(duì)象l?
          2
          3 Iterator?it? = ?l.iterator();?? // ?通過(guò)List接口對(duì)象l返回的迭代器it?
          4
          5 it.next();??????????????????? // it?有next方法?
          6
          7 it.remove();???????????????????? // ?這里會(huì)拋出?UnsupportedOperationException?
          8
          9


          PS :迭代器的作用:

          ?? 他可以以一種通用的方式去訪問(wèn)集合中的所有元素。在ArrayList類中可以通過(guò)get方法去訪問(wèn),但是有些集合的實(shí)現(xiàn)類中并沒(méi)有get方法。而我們知道,所有繼承Collection接口的接口或者這些接口的實(shí)現(xiàn)類,都可以通過(guò)iterator()返回一個(gè)迭代器,那么我們就可以通過(guò)迭代器這種通用的方式去訪問(wèn)集合中的所有元素了。訪問(wèn)方法如下:

          ?1 ArrayList?al? = ? new ?ArrayList();?
          ?2

          ?3 ????al.add( new ?Point( 3 , 3
          ));?
          ?4

          ?5 ????al.add( new ?Point( 4 , 4
          ));?
          ?6

          ?7 ????al.add( new ?Point( 5 , 5
          ));?
          ?8

          ?9 Iterator?it1? =
          ?al.iterator();?
          10

          11 ??????? while (it.hasNext())????????????????????? // ?通過(guò)迭代器訪問(wèn)集合元素?

          12 ??????? {?
          13
          ??????????????System.out.println(it.next());?
          14 ???????}
          ?
          15


          ArrayList 底層采用數(shù)組完成,而LinkedList則是以一般的雙向鏈表(double-linked list)完成,其內(nèi)每個(gè)對(duì)象除了數(shù)據(jù)本身外,還有兩個(gè)引用,分別指向前一個(gè)元素和后一個(gè)元素。

          如果我們經(jīng)常在List的開(kāi)始處增加元素,或者在List中進(jìn)行插入和刪除操作,我們應(yīng)該使用LinkedList,否則的話,使用ArrayList將更加快速。

          這兩個(gè)類都不是同步的,因此他們的效率也比較高。如果要實(shí)現(xiàn)同步,可以使用Vector類,Vector類中有一些繼承的操作,使用的時(shí)候要小心,如果不實(shí)現(xiàn)同步一般都不用Vector類。還可以用Collections類的 synchronized 相關(guān)方法實(shí)現(xiàn)同步,不過(guò)效率沒(méi)有 Vector 類高。


          評(píng)論

          # Seo Services  回復(fù)  更多評(píng)論   

          2009-05-18 06:01 by Seo Services
          Excuse me. Assuming either the Left Wing or the Right Wing gained control of the country, it would probably fly around in circles.
          I am from Nepal and now study English, tell me right I wrote the following sentence: "If seo process is not constantly going on then the site can be wiped.Create a informational website presence with upto pages."

          Thank you so much for your future answers ;-). Anders.

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 琼海市| 永泰县| 施秉县| 德格县| 巴林左旗| 英德市| 宁乡县| 庄浪县| 麦盖提县| 敦化市| 原阳县| 青海省| 凤山市| 本溪市| 阿鲁科尔沁旗| 乐山市| 卓资县| 栾城县| 潮安县| 定陶县| 皋兰县| 潜江市| 容城县| 庆云县| 沙坪坝区| 新民市| 江达县| 莫力| 鄂尔多斯市| 临沧市| 家居| 临夏县| 汉沽区| 连城县| 尤溪县| 长汀县| 葫芦岛市| 游戏| 门头沟区| 河北省| 中方县|