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

Java Vector elementAt() Method

Java Vector elementAt() Method

Java Vector Java Vector


elementAt() The method is in Java's Vector A member method provided by the class, used to obtain the element at the specified index position in the vector (Vector). This method is similar to array index access but provides more safety and flexibility.

Method Syntax:


public synchronized E elementAt(int index)

Parameters:

Return Value:

exception:


Use Cases

elementAt() The method is usually used in the following situations:

  1. When you need random access to Vector specific elements in the
  2. When accessing collection elements in an environment that requires thread safety
  3. When your code needs to interact with legacy systems (because Vector is a collection class from early Java versions)

Basic Usage Example

Here is a simple example showing how to use elementAt() method in the component:

Example

import

java.util.Vector

;

public

class

VectorExample

{

   

public

static

void

main

(

String

[

]

args

)

{

       

// Create a Vector and add elements

        Vector

<

String

fruits

=

new

Vector

<>

(

)

;

        fruits.

add

(

"Apple"

)

;

        fruits.

add

(

"Banana"

)

;

        fruits.

add

(

"Cherry"

)

;

       

       

// Use elementAt() to get an element

       

String

secondFruit

=

fruits.

elementAt

(

1

)

;

       

System

.

out

.

println

(

"The second fruit is: "

secondFruit

)

;

       

       

// Iterate over the elements in the Vector

       

for

(

int

i

=

0

;

i

<

fruits.

size

(

)

;

i

++

)

{

           

System

.

out

.

println

(

"Index "

i

" fruit: "

fruits.

elementAt

(

i

)

)

;

       

}

   

}

}

Output result:

第二个水果是: Banana
索引 0 的水果: Apple
索引 1 的水果: Banana
索引 2 的水果: Cherry

Comparison with the get() Method

Vector The class also provides get() method, which differs from elementAt() methods are similar in function:

Example

// These two approaches are equivalent

String

fruit1

=

fruits.

elementAt

(

1

)

;

String

fruit2

=

fruits.

get

(

1

)

;

Main Differences:

  1. elementAt() Is Vector is a method specific to, while get() Is List is a method defined by the interface
  2. Functionally, the two are completely identical
  3. In modern Java programming, it is more recommended to use the get() method because it conforms to the unified interface of the collections framework

Exception handling

Use elementAt() method, you should pay attention to handling possible ArrayIndexOutOfBoundsException:

Example

try

{

   

String

fruit

=

fruits.

elementAt

(

5

)

;

 

// Assume the Vector has only 3 elements

   

System

.

out

.

println

(

fruit

)

;

}

catch

(

ArrayIndexOutOfBoundsException

e

)

{

   

System

.

out

.

println

(

"Error: index out of range!"

)

;

}

A better approach is to check the index before accessing:

Example

int

index

=

5

;

if

(

index

=

0

&&

index

<

fruits.

size

(

)

)

{

   

String

fruit

=

fruits.

elementAt

(

index

)

;

   

System

.

out

.

println

(

fruit

)

;

}

else

{

   

System

.

out

.

println

(

"Index "

index

" is out of range!"

)

;

}


Performance considerations

  1. elementAt() The method's time complexity is O(1) because it can access elements directly by index
  2. Because Vector is synchronized, and theelementAt() method is also synchronized, which provides thread safety in a multithreaded environment but brings unnecessary performance overhead in a single-threaded environment
  3. If used in a single-threaded environment, consider using the ArrayList 's get() method, which has higher performance

Summary

elementAt() The method is Vector A basic operation provided by the class, used to access elements by index. Although it is functionally the same as the get() method, in modern Java development it is more recommended to use the ArrayList and get() method combination, unless you really need Vector 's thread safety features.

Remember, when using any index access method, ensure the index value is within the valid range to avoid runtime exceptions.

Java Vector Java Vector