Java Vector copyInto() 方法



描述

Java Vector copyInto(Object[] anArray) 方法用於將此向量的元件複製到指定的陣列中。此向量中索引 k處的專案將複製到陣列的元件 k中。這意味著元素在向量和陣列中的位置相同。陣列必須足夠大以容納此向量中的所有物件,否則將丟擲IndexOutOfBoundsException異常。

宣告

以下是java.util.Vector.copyInto()方法的宣告

public void copyInto(Object[] anArray)

引數

anArray - 這是將元件複製到的陣列。

返回值

返回型別為void,因此不返回任何內容。

異常

NullPointerException - 如果給定的陣列為空。

將整數向量元素複製到陣列示例

以下示例演示瞭如何使用 Java Vector copyInto() 方法將此向量的元件複製到指定的陣列中。我們正在建立一個向量物件和一個 Integer 陣列。然後將元素新增到向量和陣列中。在使用 copyinto() 方法之前列印向量和陣列的元素。然後使用 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<>(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);

      // elements in the array before copy
      System.out.println("Elements in the array before copy");
      for (Integer number : anArray) {
         System.out.println("Element = " + number);
      }

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

      // elements in the array after copy
      System.out.println("Elements in the array after copy");
      
      for (Integer number : anArray) {
         System.out.println("Element = " + number);
      }
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

Elements in the array before copy
Element = 100
Element = 100
Element = 100
Element = 100
Elements in the array after copy
Element = 4
Element = 3
Element = 2
Element = 1

將字串向量元素複製到陣列示例

以下示例演示瞭如何使用 Java Vector copyInto() 方法將此向量的元件複製到指定的陣列中。我們正在建立一個向量物件和一個 String 陣列。然後將元素新增到向量和陣列中。在使用 copyinto() 方法之前列印向量和陣列的元素。然後使用 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<String> vec = new Vector<>(4);

      String anArray[] = new String[4];

      anArray[0] = "A";
      anArray[1] = "A";
      anArray[2] = "A";
      anArray[3] = "A";

      // use add() method to add elements in the vector
      vec.add("D");
      vec.add("B");
      vec.add("C");
      vec.add("A");

      // elements in the array before copy
      System.out.println("Elements in the array before copy");
      for (String element : anArray) {
         System.out.println("Element = " + element);
      }

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

      // elements in the array after copy
      System.out.println("Elements in the array after copy");
      
      for (String element : anArray) {
         System.out.println("Element = " + element);
      }
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

Elements in the array before copy
Element = A
Element = A
Element = A
Element = A
Elements in the array after copy
Element = D
Element = C
Element = B
Element = A

將物件向量元素複製到陣列示例

以下示例演示瞭如何使用 Java Vector copyInto() 方法將此向量的元件複製到指定的陣列中。我們正在建立一個向量物件和一個 Student 陣列。然後將元素新增到向量和陣列中。在使用 copyinto() 方法之前列印向量和陣列的元素。然後使用 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<Student> vec = new Vector<>(4);

      Student anArray[] = new Student[4];

      anArray[0] = new Student(1, "Julie");
      anArray[1] = new Student(1, "Julie");
      anArray[2] = new Student(1, "Julie");
      anArray[3] = new Student(1, "Julie");

      // use add() method to add elements in the vector
      vec.add(new Student(1, "Julie"));
      vec.add(new Student(2, "Robert"));
      vec.add(new Student(3, "Adam"));
      vec.add(new Student(4, "Jane"));	  

      // elements in the array before copy
      System.out.println("Elements in the array before copy");
      for (Student element : anArray) {
         System.out.println("Element = " + element);
      }

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

      // elements in the array after copy
      System.out.println("Elements in the array after copy");
      
      for (Student element : anArray) {
         System.out.println("Element = " + element);
      }
   }
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

Elements in the array before copy
Element = [ 1, Julie ]
Element = [ 1, Julie ]
Element = [ 1, Julie ]
Element = [ 1, Julie ]
Elements in the array after copy
Element = [ 1, Julie ]
Element = [ 2, Robert ]
Element = [ 3, Adam ]
Element = [ 4, Jane ]
java_util_vector.htm
廣告