Java學習筆記27,java學習筆記
Vector是List介面的實作類別,支援List介面的全部功能,Vector類是基於數組實現的List類,在內部封裝了一個動態、
允許再分配的Object[]數組,Vector是安全執行緒的,無須程式保證該集合的同步性。
以下是Vector類的一部分方法使用說明:
public class Main {public static void main(String[] args) {Vector vector=new Vector();ArrayList list=new ArrayList();list.add("BILL");vector.add(list);//輸出:[[BILL]]System.out.println(vector);//輸出:vector容量:10System.out.println("vector容量:"+vector.capacity());/* * 增加此向量的容量(如有必要),以確保其至少能夠儲存最小容量參數指定的組件數。 * 如果此向量的當前容量小於 minCapacity,則通過將其內部資料數組(儲存在欄位 elementData 中) * 替換為一個較大的數組來增加其容量。新資料數組的大小將為原來的大小加上 capacityIncrement, * 除非 capacityIncrement 的值小於等於零,在後一種情況下,新的容量將為原來容量的兩倍, * 不過,如果此大小仍然小於 minCapacity,則新容量將為 minCapacity。 */vector.ensureCapacity(21);//輸出:vector容量:21System.out.println("vector容量:"+vector.capacity());Vector vector1=new Vector();vector1.add(list);/* * 如果指定的 Object 與此向量相等,則返回 true */System.out.println(vector.equals(vector1));//輸出:true/* * 返回此向量的第一個組件(位於索引 0) 處的項)。 */System.out.println(vector.firstElement());//輸出:[BILL]/* * 返迴向量中指定位置的元素。 * 如果索引超出範圍 (index < 0 || index >= size()),拋出ArrayIndexOutOfBoundsException 異常 */System.out.println(vector.get(0));//輸出:[BILL]/* * 返回此向量的雜湊碼值。 */System.out.println(vector.hashCode());vector.add("JACK");vector.add("MARRAY");vector.add("JACK");//輸出:[[BILL], JACK, MARRAY, JACK]System.out.println(vector);/* * 返回此向量中第一次出現的指定元素的索引,如果此向量不包含該元素,則返回 -1。更確切地講, * 返回滿足 (o==null ? get(i)==null : o.equals(get(i))) 的最低索引 i; * 如果沒有這樣的索引,則返回 -1。 */System.out.println(vector.indexOf("JACK"));//輸出:1/* * 返回此向量中第一次出現的指定元素的索引,從 index 處正向搜尋, * 如果未找到該元素,則返回 -1。 * 如果指定索引為負數,拋出IndexOutOfBoundsException異常 */System.out.println(vector.indexOf("JACK", 2));//輸出:3/* * 將指定對象作為此向量中的組件插入到指定的 index 處。 * 此向量中的每個索引大於等於指定 index 的組件都將向上移位, * 使其索引值變成比以前大 1 的值。 索引必須為一個大於等於 0 且 * 小於等於向量當前大小的值(如果索引等於向量的當前大小, * 則將新元素添加到向量)。 * 如果索引超出範圍 (index < 0 || index > size()),拋出ArrayIndexOutOfBoundsException異常 */vector.insertElementAt("WORD", 1);//輸出:[[BILL], WORD, JACK, MARRAY, JACK]System.out.println(vector);/* * 若且唯若此向量沒有組件(也就是說其大小為零)時返回 true;否則返回 false。 */System.out.println(vector.isEmpty());//輸出:false/* * 返回此向量中最後一次出現的指定元素的索引;如果此向量不包含該元素,則返回 -1。更確切地講, * 返回滿足 (o==null ? get(i)==null : o.equals(get(i))) 的最高索引 i * ;如果沒有這樣的索引,則返回 -1。 */System.out.println(vector.lastIndexOf("JACK"));//輸出:4/* * 移除此向量中指定位置的元素。將所有後續元素左移(將其索引減 1)。返回此向量中移除的元素。 * 如果索引超出範圍 (index < 0 || index >= size()),拋出ArrayIndexOutOfBoundsException 異常 */vector.remove(0);//輸出:[WORD, JACK, MARRAY, JACK]System.out.println(vector);/* * 移除此向量中指定元素的第一個匹配項,如果向量不包含該元素,則元素保持不變。 * 更確切地講,移除其索引 i 滿足 (o==null ? get(i)==null : o.equals(get(i))) * 的元素(如果存在這樣的元素)。 */System.out.println(vector.remove("WORD"));System.out.println(vector);//輸出:[JACK, MARRAY, JACK]}}
關於Vector的更多方法,請參看後續的文章
轉載請註明出處:http://blog.csdn.net/hai_qing_xu_kong/article/details/44106381 情緒控_