Java------list用法

來源:互聯網
上載者:User

標籤:方法   min   存在   false   tor   hashcode   size   string   結束   

定義list並將數組放入其中:

List<Integer> temp = new ArrayList<Integer>();

List<String> temp = new ArrayList<String>();

獲得集合內元素個數:list.size();

添加元素:
預設添加:list.add(e);
指定下標添加(添加後下標後的元素向後挪一位):list.add(index,e);

如果我們得到的List還想要增刪,那麼可以用addAll方法

       String[] str = {"1", "2", "3", };
       List<String> strArray = new ArrayList<String>();
       strArray.addAll(Arrays.asList(str));
       strArray.add("a");
     
       System.out.println(strArray);

       結果為:1 2 3 a

刪除元素:
返回是否刪除:list.remove(e);
直接刪除指定下標的元素(只刪除找到的第一個相符合的元素):list.remove(index);

替換元素(替換掉指定下標的元素):list.set(index,e);

取出元素:list.get(index);

清空集合:list.clear();

判斷集合中是否存在某個元素(存在返回true,不存在返回false):list.contains(e);

對比兩個集合中的所有元素:
兩個對象一定相等:list.equals(list2);
兩個對象不一定相等:list.hashCode() == list2.hashCode();
(兩個相等對象的equals方法一定為true, 但兩個hashcode相等的對象不一定是相等的對象。)

獲得元素下標:
元素存在則返回找到的第一個元素的下標,不存在則返回-1:list.indexOf(e);
元素存在則返回找到的最後一個元素的下標,不存在則返回-1:list.lastIndexOf(e);

判斷集合是否為空白(空則返回true,非空則返回false):list.isEmpty();

返回Iterator集合對象:list.iterator();

將集合轉換為字串:list.toString();

截取集合(從fromIndex開始在toIndex前結束,[fromIndex,toIndex)):list.subList(fromIndex,toIndex);

將集合轉換為數組:
預設類型:list.toArray();

 

Java------list用法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.