Java學習筆記28,java學習筆記

來源:互聯網
上載者:User

Java學習筆記28,java學習筆記

Vector是List介面的實作類別,支援List介面的全部功能,Vector類是基於數組實現的List類,在內部封裝了一個動態、


允許再分配的Object[]數組,Vector是安全執行緒的,無須程式保證該集合的同步性。


以下是Vector類的一部分方法使用說明:


public class Main {public static void main(String[] args) {Vector vector = new Vector();vector.add("BILL");vector.add("JACK");// 輸出:[BILL, JACK]System.out.println(vector);// 輸出:vector容量:10System.out.println("vector容量:" + vector.capacity());/* * 設定此向量的大小。如果新大小大於當前大小, 則會在向量的末尾添加相應數量的 null 項。 如果新大小小於當前大小,則丟棄索引 * newSize 處及其之後的所有項。 如果新大小為負數,拋出ArrayIndexOutOfBoundsException 異常 */vector.setSize(21);// 輸出:vector容量:21System.out.println("vector容量:" + vector.capacity());/* * 返回此向量的容量 */System.out.println(vector.size());// 輸出:21/* * 返回此 List 的部分視圖,元素範圍為從 fromIndex(包括)到 toIndex(不包括)。 (如果 fromIndex 和 * toIndex 相等,則返回的 List 將為空白)。 返回的 List 由此 List 支援,因此返回 List 中的更改將反映在此 * List 中,反之亦然。 返回的列表支援此列表支援的所有可選列表操作。 */List list = (List) vector.subList(0, 1);// 輸出:[BILL]System.out.println(list.toString());/* * 返回一個數組,包含此向量中以恰當順序存放的所有元素。 */Object[] obj = (Object[]) vector.toArray();// 輸出:BILL JAC null null null null null null// null null null null null null null null null// null null null nullfor (Object ob : obj) {System.out.println(ob);}vector.setSize(5);// 設定向量容量為5String[] str = new String[6];/* * 返回一個數組,包含此向量中以恰當順序存放的所有元素; 返回數組的運行時類型為指定數組的類型。 如果向量能夠適應指定的數組,則返回該數組。 * 否則使用此數組的運行時類型和此向量的大小分配一個新數組。 注意:當返回的數組大小小於向量的容量時,返回為null的數組; * 當返回的數組的大小和向量的容量相等,將向量元素放入數組中; 如果返回的數組的大小比向量的容量還要大,將向量元素放入數組中, * 後面剩餘的位置為null。 */vector.toArray(str);// 輸出:[BILL, JACK, null, null, null, null]System.out.println(Arrays.toString(str));/* * 返回此向量的字串表示形式,其中包含每個元素的 String 表示形式。 */// 輸出:[BILL, JACK, null, null, null]System.out.println(vector.toString());/* * 對此向量的容量進行微調,使其等於向量的當前大小。 */vector.trimToSize();// 輸出:5System.out.println(vector.capacity());/* * 將此向量指定 index 處的組件設定為指定的對象。丟棄該位置以前的組件。 如果索引超出範圍 (index < 0 || index >= * size()), 拋出ArrayIndexOutOfBoundsException異常 */vector.setElementAt("Marry", 3);// 輸出:[BILL, JACK, null, Marry, null]System.out.println(vector);/* * 用指定的元素替換此向量中指定位置處的元素。 如果索引超出範圍 (index < 0 || index >= size()) * 拋出ArrayIndexOutOfBoundsException異常 */vector.set(3, "Change");// 輸出:[BILL, JACK, null, Change, null]System.out.println(vector);/* * 從此向量中移除全部組件,並將其大小設定為零。 */vector.removeAllElements();System.out.println(vector.size());//輸出0}}

轉載請註明出處:http://blog.csdn.net/hai_qing_xu_kong/article/details/44119233  情緒控_ 



聯繫我們

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