이 기사에서는 목록에서 최소값과 최대값을 얻는 방법을 이해합니다. 목록은 요소를 순차적으로 저장하고 액세스할 수 있도록 하는 정렬된 컬렉션입니다. 여기에는 요소를 삽입, 업데이트, 삭제 및 검색하는 인덱스 기반 메서드가 포함되어 있습니다. 중복 요소를 가질 수도 있습니다.
아래는 동일한 데모입니다 -
입력이 다음과 같다고 가정 -
Input list: [500, 650, 300, 250, 110]
원하는 출력은 -
The minimum value of the list is: 110 The maximum value of the list is: 650
알고리즘
Step 1 - START Step 2 - Declare a list namely input_list. Step 3 - Define the values. Step 4 - Sort the list using the inbuilt function .sort(). Step 5 - Use the inbuilt function Integer.MAX_VALUE to fetch the max value of the list and Integer.MIN_VALUE to fetch the minimum value of the list. Step 6 - Display the result Step 7 - Stop
예시 1
여기에서 모든 작업을 'main' 기능 아래에 묶습니다.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Demo {
public static void main(String[] args) {
System.out.println("Required packages have been imported");
List<Integer> input_list = new ArrayList<>();
input_list.add(500);
input_list.add(650);
input_list.add(300);
input_list.add(250);
input_list.add(110);
System.out.println("The list is defined as " +input_list);
List<Integer> sortedlist = new ArrayList<>(input_list);
Collections.sort(sortedlist);
if (sortedlist == null || sortedlist.size() == 0) {
System.out.println("\nThe minimum value of the list is: " +Integer.MAX_VALUE);
}
System.out.println("\nThe minimum value of the list is: " +sortedlist.get(0));
if (sortedlist == null || sortedlist.size() == 0) {
System.out.println("The maximum value of the list is: " + Integer.MIN_VALUE);
return ;
}
int list_size = sortedlist.size() - 1;
System.out.println("The maximum value of the list is: " + sortedlist.get(list_size));
}
} 출력
Required packages have been imported The list is defined as [500, 650, 300, 250, 110] The minimum value of the list is: 110 The maximum value of the list is: 650
예시 2
여기에서 객체 지향 프로그래밍을 나타내는 함수로 작업을 캡슐화합니다.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Demo {
public static Integer get_min_value(List<Integer> sortedlist) {
if (sortedlist == null || sortedlist.size() == 0) {
return Integer.MAX_VALUE;
}
return sortedlist.get(0);
}
public static Integer get_max_value(List<Integer> sortedlist) {
if (sortedlist == null || sortedlist.size() == 0) {
return Integer.MIN_VALUE;
}
int list_size = sortedlist.size() - 1;
return sortedlist.get(list_size);
}
public static void main(String[] args) {
System.out.println("Required packages have been imported");
List<Integer> input_list = new ArrayList<>();
input_list.add(500);
input_list.add(650);
input_list.add(300);
input_list.add(250);
input_list.add(110);
System.out.println("The list is defined as " +input_list);
List<Integer> sortedlist = new ArrayList<>(input_list);
Collections.sort(sortedlist);
System.out.println("\nThe minimum value of the list is: " + get_min_value(sortedlist));
System.out.println("The maximum value of the list is: " + get_max_value(sortedlist));
}
} 출력
Required packages have been imported The list is defined as [500, 650, 300, 250, 110] The minimum value of the list is: 110 The maximum value of the list is: 650