java.util.Vector.clone()


描述

clone() 方法被用来获得的现有vector.The拷贝另一个副本将有一个参考到内部数据阵列的克隆,但不是一个参考到原始内部数据数组。

声明

以下是java.util.Vector.clone()方法的声明

public Object clone()

参数

此方法不接受任何参数作为输入。

返回值

Object - 方法调用返回此向量的克隆作为对象。

异常

CloneNotSupportedException - 如果对象的类不支持Cloneable接口,则抛出此异常。

实例

以下示例显示了java.util.Vector.clone()方法的用法。

package com.tutorialspoint;

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