當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java java.util.Vector.copyInto()用法及代碼示例


描述

這個copyInto(Object[] anArray)方法用於將此向量的分量複製到指定的數組中。該項目在index k在這個向量中被複製到component k的數組。這意味著元素在向量和數組中的位置相同。該數組必須足夠大以容納此向量中的所有對象,否則將引發 IndexOutOfBoundsException。

聲明

以下是聲明java.util.Vector.copyInto()方法

public void copyInto(Object[] anArray)

參數

anArray─ 這是將組件複製到其中的數組。

返回值

返回類型是void所以不返回任何東西。

異常

NullPointerException- 如果給定的數組為空。

示例

下麵的例子展示了 java.util.Vector.copyInto() 方法的用法。

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);

      Integer anArray[] = new Integer[4];

      anArray[0] = 100;
      anArray[1] = 100;
      anArray[2] = 100;
      anArray[3] = 100;

      // use add() method to add elements in the vector
      vec.add(4);
      vec.add(3);
      vec.add(2);
      vec.add(1);

      // numbers in the array before copy
      System.out.println("Numbers in the array before copy");
      for (Integer number:anArray) {         
         System.out.println("Number = " + number);
      }

      // copy into the array
      vec.copyInto(anArray);

      // numbers in the array after copy
      System.out.println("Numbers in the array after copy");
      
      for (Integer number:anArray) {         
         System.out.println("Number = " + number);
      }
   }
}

讓我們編譯並運行上麵的程序,這將產生以下結果。

Numbers in the array before copy
Number = 100
Number = 100
Number = 100
Number = 100
Numbers in the array after copy
Number = 4
Number = 3
Number = 2
Number = 1

相關用法


注:本文由純淨天空篩選整理自 java.util.Vector.copyInto() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。