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

Java Vector setSize() Method

Java Vector setSize() Method

Java Vector Java Vector


setSize() is a method in Java's Vector An important method provided by the class, used to set a new size for the vector. This method can dynamically adjust the Vector 's capacity so that it meets the needs of the running program.

Method Syntax:


public void setSize(int newSize)

Method parameters

Parameter Description

Parameter Characteristics


Method behavior

Expansion Case

When the new size is greater than the current size:

Example

Vector

<

String

vector

=

new

Vector

<>

(

Arrays

.

asList

(

"A"

,

"B"

,

"C"

)

)

;

vector.

setSize

(

5

)

;

// Now vector contains ["A", "B", "C", null, null]

Shrinking Case

When the new size is less than the current size:

Example

Vector

<

String

vector

=

new

Vector

<>

(

Arrays

.

asList

(

"A"

,

"B"

,

"C"

,

"D"

)

)

;

vector.

setSize

(

2

)

;

// Now vector contains ["A", "B"]

Equal Case

When the new size equals the current size:

Example

Vector

<

String

vector

=

new

Vector

<>

(

Arrays

.

asList

(

"A"

,

"B"

,

"C"

)

)

;

vector.

setSize

(

3

)

;

// vector remains unchanged, still ["A", "B", "C"]


Usage examples

Basic example

Example

import

java.util.Vector

;

public

class

VectorSetSizeExample

{

   

public

static

void

main

(

String

[

]

args

)

{

       

// Create an initial Vector

        Vector

<

Integer

numbers

=

new

Vector

<>

(

)

;

        numbers.

add

(

10

)

;

        numbers.

add

(

20

)

;

        numbers.

add

(

30

)

;

       

       

System

.

out

.

println

(

"Original Vector: "

numbers

)

;

       

       

// Expand the Vector

        numbers.

setSize

(

5

)

;

       

System

.

out

.

println

(

"Vector after expansion: "

numbers

)

;

       

       

// Shrink the Vector

        numbers.

setSize

(

2

)

;

       

System

.

out

.

println

(

"Vector after shrinking: "

numbers

)

;

   

}

}

Output result:

原始 Vector: [10, 20, 30]
扩大后的 Vector: [10, 20, 30, null, null]
缩小后的 Vector: [10, 20]

Practical Application Scenarios

setSize() The method is especially useful in the following scenarios:

  1. Pre-allocating Space: When you know you need to store a certain number of elements, you can set the size in advance
  2. Cleaning Up Excess Space: When some elements are no longer needed, you can shrink the Vector to save memory
  3. Resetting the Container: Quickly clear the Vector and set a new size

Example

// Pre-allocation example

Vector

<

String

names

=

new

Vector

<>

(

)

;

names.

setSize

(

100

)

;

// Pre-allocate 100 positions

// Cleaning up excess space example

Vector

<

Double

prices

=

new

Vector

<>

(

)

;

// ... after adding many elements ...

prices.

setSize

(

10

)

;

// Keep only the first 10 elements

// Container reset example

Vector

<

Object

tempStorage

=

new

Vector

<>

(

)

;

// ... after use ...

tempStorage.

setSize

(

0

)

;

// Clear all elements


Notes

Performance considerations

Thread Safety

Alternatives

In the Java collections framework,ArrayList is a more modern alternative, but it does not have a direct setSize() method. If similar functionality is needed, consider:

Example

// Implement similar functionality using ArrayList

ArrayList

<

String

list

=

new

ArrayList

<>

(

)

;

// Expand

while

(

list.

size

(

)

<

newSize

)

{

    list.

add

(

null

)

;

}

// Shrink

while

(

list.

size

(

)

newSize

)

{

    list.

remove

(

list.

size

(

)

1

)

;

}


Summary

Vector 's setSize() method provides a direct way to adjust the vector's size, whether expanding or shrinking. Although Vector in modern Java programming is not as ArrayList commonly used as, it still has value in certain specific scenarios (such as needing thread safety or maintaining legacy code). Understanding this method's behavior and characteristics can help you use it more effectively Vector collections.

Java Vector Java Vector