java.util.Vector.clone()方法實例
clone() 方法用於獲得一個現有的載體的另一個副本。該副本將有一個參考的內部數據數組的克隆而不是參考的原始內部數據數組。
聲明
以下是java.util.Vector.clone()方法的聲明
public Object clone()
參數
-
此方法不帶任何參數作為輸入。
返回值
Object--該方法調用返回此向量為對象的克隆。
異常
-
CloneNotSupportedException-- 拋出此異常,如果對象的類不支持Cloneable接口。
例子
下麵的例子顯示java.util.Vector.clone()方法的使用。
package com.yiibai; import java.util.Vector; public class VectorDemo { public static void main(String[] args) { // create an empty Vector vec with an initial capacity of 4 Vector<Integer> vec = new Vector<Integer>(4); Vector<Integer> vecclone = new Vector<Integer>(4); // use add() method to add elements in the vector vec.add(4); vec.add(3); vec.add(2); vec.add(1); // let us print all the elements available in vec System.out.println("Added numbers in vec vector are :- "); for (Integer number : vec) { System.out.println("Number = " + number); } // let us clone the vector vec vecclone=(Vector)vec.clone(); // let us print all the elements available in vecclone System.out.println("Added numbers in vecclone vector are :-"); for (Integer number : vecclone) { System.out.println("Number = " + number); } } }
現在編譯和運行上麵的代碼示例,將產生以下結果。
Added numbers in vec vector are :- Number = 4 Number = 3 Number = 2 Number = 1 Added numbers in vecclone vector are :- Number = 4 Number = 3 Number = 2 Number = 1