java中的資料結構–list

來源:互聯網
上載者:User
java.util
介面 List<E>
所有超級介面:
Collection<E>,
Iterable<E>
所有已知實作類別:
AbstractList,
AbstractSequentialList, ArrayList,
AttributeList, CopyOnWriteArrayList,
LinkedList,
RoleList, RoleUnresolvedList,
Stack, Vector

 

 

方法摘要
 boolean add(E e)
          向列表的尾部添加指定的元素(可選操作)。
 void add(int index, E element)
          在列表的指定位置插入指定元素(可選操作)。
 boolean addAll(Collection<? extends E> c)
          添加指定 collection 中的所有元素到此列表的結尾,順序是指定 collection 的迭代器返回這些元素的順序(可選操作)。
 boolean addAll(int index, Collection<? extends E> c)
          將指定 collection 中的所有元素都插入到列表中的指定位置(可選操作)。
 void clear()
          從列表中移除所有元素(可選操作)。
 boolean contains(Object o)
          如果列表包含指定的元素,則返回 true。
 boolean containsAll(Collection<?> c)
          如果列表包含指定 collection 的所有元素,則返回 true。
 boolean equals(Object o)
          比較指定的對象與列表是否相等。
 E get(int index)
          返回列表中指定位置的元素。
 int hashCode()
          返回列表的雜湊碼值。
 int indexOf(Object o)
          返回此列表中第一次出現的指定元素的索引;如果此列表不包含該元素,則返回 -1。
 boolean isEmpty()
          如果列表不包含元素,則返回 true。
 Iterator<E> iterator()
          返回按適當順序在列表的元素上進行迭代的迭代器。
 int lastIndexOf(Object o)
          返回此列表中最後出現的指定元素的索引;如果列表不包含此元素,則返回 -1。
 ListIterator<E> listIterator()
          返回此列表元素的列表迭代器(按適當順序)。
 ListIterator<E> listIterator(int index)
          返回列表中元素的列表迭代器(按適當順序),從列表的指定位置開始。
 E remove(int index)
          移除列表中指定位置的元素(可選操作)。
 boolean remove(Object o)
          從此列表中移除第一次出現的指定元素(如果存在)(可選操作)。
 boolean removeAll(Collection<?> c)
          從列表中移除指定 collection 中包含的其所有元素(可選操作)。
 boolean retainAll(Collection<?> c)
          僅在列表中保留指定 collection 中所包含的元素(可選操作)。
 E set(int index, E element)
          用指定元素其他清單中指定位置的元素(可選操作)。
 int size()
          返回列表中的元素數。
 List<E> subList(int fromIndex, int toIndex)
          返回列表中指定的 fromIndex(包括 )和 toIndex(不包括)之間的部分視圖。
 Object[] toArray()
          返回按適當順序包含列表中的所有元素的數組(從第一個元素到最後一個元素)。
<T> T[]
 toArray(T[] a)
          返回按適當順序(從第一個元素到最後一個元素)包含列表中所有元素的數組;返回數組的運行時類型是指定數組的運行時類型。

 

package com.mwq;import java.util.ArrayList;import java.util.LinkedList;import java.util.Iterator;import java.util.List;public class TestCollection {public static void main(String[] args) {System.out.println("開始:");String a = "A", b = "B", c = "C", d = "D", e = "E";List<String> list = new LinkedList<String>();list.add(a);list.add(e);list.add(d);list.set(1, b);// 將索引位置為1的對象e修改為對象blist.add(2, c);// 將對象c添加到索引位置為2的位置Iterator<String> it = list.iterator();while (it.hasNext()) {System.out.println(it.next());}//                 for (int i = 0; i < list.size(); i++) {//                       System.out.println(list.get(i));// 利用get(int index)方法獲得指定索引位置的對象//          }System.out.println("結束!");}}package com.mwq;import java.util.ArrayList;import java.util.List;public class TestCollection {public static void main(String[] args) {System.out.println("開始:");String a = "A", b = "B", c = "C", d = "D", repeat = "Repeat";List<String> list = new ArrayList<String>();list.add(a); // 索引位置為 0list.add(repeat); // 索引位置為 1list.add(b); // 索引位置為 2list.add(repeat); // 索引位置為 3list.add(c); // 索引位置為 4list.add(repeat); // 索引位置為 5list.add(d); // 索引位置為 6System.out.println(list.indexOf(repeat));System.out.println(list.lastIndexOf(repeat));System.out.println(list.indexOf(b));System.out.println(list.lastIndexOf(b));System.out.println("結束!");}}在控制台將輸出如下資訊:1522package com.mwq;import java.util.ArrayList;import java.util.List;public class TestCollection {public static void main(String[] args) {System.out.println("開始:");String a = "A", b = "B", c = "C", d = "D", e = "E";List<String> list = new ArrayList<String>();list.add(a); // 索引位置為 0list.add(b); // 索引位置為 1list.add(c); // 索引位置為 2list.add(d); // 索引位置為 3list.add(e); // 索引位置為 4list = list.subList(1, 3);// 利用從索引位置 1 到 3 的對象重建一個List集合for (int i = 0; i < list.size(); i++) {System.out.println(list.get(i));}System.out.println("結束!");}}在控制台將輸出如下資訊:BC

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.