在 Java 数组列表中寻找最小值或最大值
原文:https://www . geesforgeks . org/find-Java 中的最小值或最大值-arraylist/
最小值为最小值,最大值为最大值。这里的主要任务是从数组列表中找到最小值和最大值。考虑一个数组列表的例子,我们需要找到最大和最小的元素。
例:
Input List: {10, 20, 8, 32, 21, 31};
Output:
Maximum is: 32
Minimum is: 8
方法 1:通过迭代数组列表值
- First, we need to initialize the array list value.
- Then use the size () function to find the length of the array list.
- After that, the first element of the array list will be stored in the variables min and max.
- Then use the for loop to iterate through the array list elements one by one to find the minimum and maximum values from the array list.
Java
// Java program to find the minimum and
// maximum value of the ArrayList
import java.util.*;
public class Max {
public static void main(String args[])
{
// initializing the ArrayList elements
ArrayList<Integer> arr = new ArrayList<>();
arr.add(10);
arr.add(20);
arr.add(8);
arr.add(32);
arr.add(21);
arr.add(31);
int min = arr.get(0);
int max = arr.get(0);
// store the length of the ArrayList in variable n
int n = arr.size();
// loop to find minimum from ArrayList
for (int i = 1; i < n; i++) {
if (arr.get(i) < min) {
min = arr.get(i);
}
}
// loop to find maximum from ArrayList
for (int i = 1; i < n; i++) {
if (arr.get(i) > max) {
max = arr.get(i);
}
}
// The result will be printed
System.out.println("Maximum is : " + max);
System.out.println("Minimum is : " + min);
}
}
输出
Maximum is : 32
Minimum is : 8
版权属于:月萌API www.moonapi.com,转载请注明出处