Java Example – Getting the Maximum and Minimum Values of an Array
The following example demonstrates how to use the Collections.max() and Collections.min() methods of the Collections class to find the maximum and minimum values in an array:
Main.java File
import
java
.
util
.
Arrays
;
import
java
.
util
.
Collections
;
public
class
Main
{
public
static
void
main
(
String
[
]
args
)
{
Integer
[
]
numbers
=
{
8
,
2
,
7
,
1
,
4
,
9
,
5
}
;
int
min
=
(
int
)
Collections
.
min
(
Arrays
.
asList
(
numbers
)
)
;
int
max
=
(
int
)
Collections
.
max
(
Arrays
.
asList
(
numbers
)
)
;
System
.
out
.
println
(
"
Minimum value:
"
min
)
;
System
.
out
.
println
(
"
Maximum value:
"
max
)
;
}
}
The output of the above code is:
最小值: 1
最大值: 9
Java Examples