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

Java Example – Getting the Index Value of a Vector Element

Java Example – Getting the Index Value of a Vector Element

Java Examples Java Examples

The following example demonstrates using the sort() method of the Collections class to sort the vector and the binarySearch() method to get the index value of a vector element:

Main.java File

import

java

.

util

.

Collections

;

import

java

.

util

.

Vector

;

public

class

Main

{

public

static

void

main

(

String

[

]

args

)

{

Vector

v

=

new

Vector

(

)

;

v

.

add

(

"

X

"

)

;

v

.

add

(

"

M

"

)

;

v

.

add

(

"

D

"

)

;

v

.

add

(

"

A

"

)

;

v

.

add

(

"

O

"

)

;

Collections

.

sort

(

v

)

;

System

.

out

.

println

(

v

)

;

int

index

=

Collections

.

binarySearch

(

v

,

"

D

"

)

;

System

.

out

.

println

(

"

The element index value is:

"

index

)

;

}

}

The output of the above code is:



[A, D, M, O, X]

元素索引值为 : 1

Java Examples Java Examples