標籤:api random arraylist java
根據java1.6 的API整理一下Arraylist的幾個常用方法。
三個建構函式
1.public ArrayList(int initialCapacity);
構造一個具有指定初始容量的空列表
2.pubilc ArrayList();
構造一個初始容量為10的空列表
3.public ArrayList(Collection<> c)
構造一個包含指定collection的元素的列表。
若collection為null,會拋出NullPointerException
其他常用方法
4.trimToSize
public void trimToSize();
將此ArrayList的容量調整為列表的當前大小。應用程式可以使用此操作來最小化ArrayList 執行個體的儲存量。
5.size
public int size();
返回此列表中的元素數。
6.isEmpty
public boolean isEmpty();
如果此列表中沒有元素,則返回 true
7.contains
public boolean contains(Object o);
如果此列表中包含指定的元素,則返回 true
8.indexOf
public int indexOf(Object o)
返回此列表中首次出現的指定元素的索引,或如果此列表不包含元素,則返回 -1。
9.lastIndexOf
public int lastIndexOf(Object o)
返回此列表中最後一次出現的指定元素的索引,或如果此列表不包含索引,則返回 -1。
10.toArray
public Object[] toArray();
按適當順序(從第一個到最後一個元素)返回包含此列表中所有元素的數組。
由於此列表不維護對返回數組的任何引用,,因而它將是“安全的”。(換句話說,此方法必須分配一個新的數組)。因此,調用者可以自由地修改返回的數組。
此方法擔當基於數組的 API 和基於 collection 的 API 之間的橋樑。
11.get
public E get(int index);
返回此列表中指定位置上的元素
12.set
public E set(int index, E element);
用指定的元素替代此列表中指定位置上的元素。返回值為以前位於該指定位置上的元素
13.add
public boolean add(E element);
將指定的元素添加到此列表的尾部。添加成功返回true
14.add
public void add(int index, E element)
將指定的元素插入此列表中的指定位置。向右移動當前位於該位置的元素(如果有)以及所有後續元素(將其索引加 1)。
15.remove
public E remove(int index)
移除此列表中指定位置的元素,返回從列表中移除的元素
16.remove
public boolean remove(Object o)
移除此列表中首次出現的指定元素(如果存在)。如果列表不包含此元素,則列表不做改動。
17.clear
public void clear()
移除此列表中的所有元素。此調用返回後,列表將為空白
18.addAll
public boolean addAll(Collection c)
按照指定 collection 的迭代器所返回的元素順序,將該 collection 中的所有元素添加到此列表的尾部
19.addAll
public boolean addAll(int index, Collection c)
從指定的位置開始,將指定 collection 中的所有元素插入到此列表中。
20.removeRange
protected void removeRange(int fromIndex, int endIndex);
移除列表中索引在 fromIndex(包括)和toIndex(不包括)之間的所有元素。向左移動所有後續元素(減小其索引)。此調用將列表縮短了 (toIndex - fromIndex) 個元素。(如果toIndex==fromIndex,則此操作無效。)
package collection;import java.util.ArrayList;import java.util.List;import java.util.Random;public class arraylistTest {public static Random random = new Random();public static char[] charArray = {};public static void main(String [] args){List<String> list = new ArrayList<String>();//往list加入10個隨機字串for(int i = 0;i < 10 ;i++){list.add(arraylistTest.GenRandomString(5));}//往list中插入一個固定字串list.add("abc");//1.判斷arraylist是否為空白boolean isEmpty = list.isEmpty();System.out.println("arraylist是否為空白:" + isEmpty);//2.返回列表中的元素數int size = list.size();System.out.println("arraylist中的元素個數為:" + size);//3.判斷arrayList中是否含有指定元素boolean isExist = list.contains("abc");System.out.println("arraylist中是否含有abc:" + isExist);//4.返回arraylist中元素第一次出現的位置,若沒有則返回-1int index = list.indexOf("abc");System.out.println("arraylist中第一次出現abc的位置是" + index);//5.返回arraylist中元素最後一次出現的位置,若沒有則返回-1int lastIndex = list.lastIndexOf("abc");System.out.println("arraylist最後一次出現abc的位置是" + lastIndex);//6.返回包含此列表中所有元素的數組(按順序),相當於數組 API和collection API的橋樑,返回一個object的數組Object[] objectArray = list.toArray();System.out.println("產生的數組為");for(Object obj : objectArray){System.out.println(obj);}//7.返回列表中指定位置的元素,如果超出返回,則會拋出IndexOutOfBoundsExceptionString s = list.get(0);System.out.println("列表中第一個元素是:" + s);//8.用指定的元素替代此列表中指定位置上的元素,傳回值是以前位於該位上的元素String old = list.set(0, "abc");System.out.println("第一個元素" + old + "被替換成了abc");//9.將元素插入到列表的尾部System.out.println("是否插入成功:" + list.add("def"));//10.將元素插入到指定位置,傳回型別為voidlist.add(0, "ghi");//11.移除指定位置的元素System.out.println("元素" + list.remove(0) + "已被移除");//12.移除此列表中首次出現的指定元素(如果存在)System.out.println("元素是否被移除:" + list.remove("abc"));//13.按照指定 collection 的迭代器所返回的元素順序,將該 collection 中的所有元素添加到此列表的尾部List<String> list2 = new ArrayList<String>();list2.add(arraylistTest.GenRandomString(5));list2.add(arraylistTest.GenRandomString(5));list.addAll(list2);System.out.println("新列表為:");for(String str: list){System.out.println(str);}//14.移除列表中所有元素list.clear();System.out.println("移除所有元素後列表的長度為:" + list.size());}//產生指定長度的隨機字串public static String GenRandomString(int length){charArray = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();char[] randomChar = new char [length];for(int i = 0; i < randomChar.length; i++){randomChar[i] = charArray[random.nextInt(61)];}return new String(randomChar);}}
java API中提供的ArrayList的常用方法