天天看點

一起Talk Android吧(第八十五回:Java中的類集之List)

各位看官們,大家好,上一回中咱們說的是Java中類集的例子,這一回咱們說的例子是Java中的類集之List。閑話休提,言歸正轉。讓我們一起Talk Android吧!

看官們,我們在上一章回中對Java中的類集做了概述性的介紹,這一回中我們将對類集中具體的接口和類進行介紹,這一回主要介紹

List接口和它的實作類ArrayList

。這個

List

就是我們在資料結構中的清單,或者叫線性表。

List

Collection

接口的子接口,

ArrayList

是它的實作類,接下來我們看看如何使用它們。

看官們,對清單的操作可以簡單概括為:擷取,添加,修改和删除,這些操作都有相對應的方法。

  • 從清單中擷取元素的方法:

    obj get(index);

  • 向清單中添加元素的方法:

    add(obj),add(index,obj);

  • 修改清單中元素的方法:

    set(index,obj);

  • 删除清單中元素的方法:

    remove(index);remove(obj)

這些方法有一個

obj

參數,它表示清單中的元素。接下來我們通過具體的代碼來介紹如何使用這些方法來操作清單。

public class ListEx {
    public static void main(String args[]){
        //init ArrayLisst
        List<Integer> list = new ArrayList<>(); 
        for(int i=; i<;++i){
            //list.add(new Integer(i+1));
            list.add((i+));
        }

        //show size of list ,and content of list
        System.out.println("size of list: "+list.size());
        for(Integer i:list)
            System.out.print(i+" ");

        System.out.println();
        System.out.println("list: "+list);

        //change list to array
        Integer [] array = list.toArray(new Integer [] {});
        System.out.println("change list to array: "+Arrays.toString(array));

        //add a content at location 8 of list
        list.add(,);
        System.out.println("add 6 at location 8 of list: "+list);

        //get the content of list
        System.out.println("the first content is: "+list.get());

        //change the content of list
        System.out.println("change the first content from "+list.get()+" to 9.");
        list.set(,);
        System.out.println("list: "+list);

        //get the location of content
        System.out.println("the content 3 is located: "+list.indexOf());

        //delete the content of list,this is based on content of list
        list.remove(new Integer());
        System.out.println("after removing the content 9: "+list);

        //delete the content of list,this is based on location of list
        list.remove();
        System.out.println("after removing the 9th content: "+list);
    }
}
           

關于這些方法我說一下注意事項:

  • add

    remove

    方法都是重載方法,可以依據具體情況來選擇。
  • 添加元素時如果不指定添加的位置,那麼預設在清單末尾添加元素。
  • 删除元素時如何想删除指定位置的元素,那麼需要确定該位置是有效的位置,不然會有異常。

比如清單長度為5,也就是說有5個位置存放元素,那麼大于4或者小于0的位置都是無效位置。清單中元素的位置和數組中元素的下标是相同的,是以在進行清單中有關位置的操作時,可以把位置看作是數組中的下标。

下面是程式的運作結果,請參考:

size of list: 
          
list: [, , , , , , , , , ]
change list to array: [, , , , , , , , , ]
add  at location  of list: [, , , , , , , , , , ]
the first content is: 
change the first content from  to 
list: [, , , , , , , , , , ]
the content  is located: 
after removing the content : [, , , , , , , , , ]
after removing the th content: [, , , , , , , , ]
           

看官們,

ArrayList

底層使用數組來實作,不過與數組相比,它的大小可以動态調節,這也是它最受歡迎的地方,我們隻需要往裡面添加元素就行,不用管它是否可以容納的下這些資料,真是個“大胃王”呀。此外,它也可以轉換為數組。

List

接口的實作類中還有一個

Vector

類,它類似

ArrayList

類,不過它主要用線上程安全的操作中。還有就是一些老的代

碼中,因為它比

ArrayList

誕生的早。在實際項目中,除非有線程安全的需要,否則使用

ArrayList

就可以。

各位看官,關于Java中類集之List的例子咱們就介紹到這裡,欲知後面還有什麼例子,且聽下回分解!

繼續閱讀