Java Example – Getting the Maximum Element of a Vector
The following example demonstrates using the v.add() method of the Vector class and the Collections.max() method of the Collection class to get the maximum element of the vector:
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
(
new
Double
(
"
3.4324
"
)
)
;
v
.
add
(
new
Double
(
"
3.3532
"
)
)
;
v
.
add
(
new
Double
(
"
3.342
"
)
)
;
v
.
add
(
new
Double
(
"
3.349
"
)
)
;
v
.
add
(
new
Double
(
"
2.3
"
)
)
;
Object
obj
=
Collections
.
max
(
v
)
;
System
.
out
.
println
(
"
The maximum element is:
"
obj
)
;
}
}
The output of the above code is:
最大元素是:3.4324
Java Examples