Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Vector copyInto() Method

Java Vector copyInto() Method

Java Vector Java Vector


copyInto() is a method in Java's Vector A practical method provided by the class, which allows us to Vector all elements into a specified array. This method is very useful in scenarios where you need to convert the Vector contents into an array.

Method Syntax:


public void copyInto(Object[] anArray)

Parameter Description

- The array type must be `Object[]` - The array length must be at least equal to the size of the `Vector`

Return Value

This method has no return value (void type); it only performs the copy operation.


Usage examples

Basic Usage

Example

import

java.util.Vector

;

public

class

CopyIntoExample

{

   

public

static

void

main

(

String

[

]

args

)

{

       

// Create a Vector

        Vector

<
String
vector
=
new
Vector
<>
(
)

;

        vector.

add

(

"Apple"

)

;

        vector.

add

(

"Banana"

)

;

        vector.

add

(

"Cherry"

)

;

       

       

// Create a sufficiently large array

       

String

[

]

fruits

=

new

String

[

vector.

size

(

)

]

;

       

       

// Use the copyInto() method

        vector.

copyInto

(

fruits

)

;

       

       

// Print the array contents

       

for

(

String

fruit

:

fruits

)

{

           

System

.

out

.

println

(

fruit

)

;

       

}

   

}

}

Output result:

Apple
Banana
Cherry

Notes

  1. Insufficient Array Size: If the destination array length is less than Vector the size of the, a IndexOutOfBoundsException

Example

Vector

<
Integer
numbers
=
new
Vector
<>
(
)

;

numbers.

add

(

1

)

;

numbers.

add

(

2

)

;

numbers.

add

(

3

)

;

Integer

[

]

smallArray

=

new

Integer

[

2

]

;

// The array is too small

numbers.

copyInto

(

smallArray

)

;

// Throws IndexOutOfBoundsException

  1. Type Safety: Starting from Java 5, it is recommended to use generics to ensure type safety

Example

Vector

<
String
stringVector
=
new
Vector
<>
(
)

;

stringVector.

add

(

"Hello"

)

;

// Unsafe practice

Object

[

]

objArray

=

new

Object

[

stringVector.

size

(

)

]

;

stringVector.

copyInto

(

objArray

)

;

// Better practice

String

[

]

strArray

=

new

String

[

stringVector.

size

(

)

]

;

stringVector.

copyInto

(

strArray

)

;

// Requires a type conversion warning


Comparison with Other Methods

Differences from toArray()

Vector The class also provides toArray() method, which differs from copyInto() in the following ways:

Method Whether a new array is created Whether an exception may be thrown Use Cases
copyInto() No, uses the existing array Yes (array too small) When an array of suitable size already exists
toArray() Yes, creates a new array No When a new array is needed

Performance considerations


Practical Application Scenarios

  1. Interacting with Legacy Code: When you need to pass the Vector contents to old code that only accepts arrays
  2. Batch Operations: When you need to perform batch processing on the Vector contents
  3. Performance optimization: When the array size is known and the array needs to be reused

Summary

Vector.copyInto() is a simple but practical method that provides the ability to copy Vector contents into an existing array. Although in new code ArrayList is more commonly used, understanding this method is still valuable when maintaining old code or in specific scenarios.

Key points review:

Java Vector Java Vector