Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Java

Chương trình Java lấy giá trị Min và Max từ List


Trong bài viết này, chúng ta sẽ hiểu cách lấy giá trị tối thiểu (Min) và tối đa (Max) từ List. List là collection có thứ tự cho phép lưu trữ và truy cập phần tử theo trình tự. Nó chứa các phương thức dựa trên chỉ số để chèn, cập nhật, xóa và tìm kiếm. Nó cũng có thể chứa phần tử trùng lặp.

Minh họa dưới đây:

Giả sử đầu vào là:

Input list: [500, 650, 300, 250, 110]

Đầu ra mong muốn:

The minimum value of the list is: 110 The maximum value of the list is: 650

Thuật toán

Bước 1 - BẮT ĐẦU 
Bước 2 - Khai báo list input_list 
Bước 3 - Định nghĩa giá trị 
Bước 4 - Sắp xếp list bằng hàm built-in .sort() 
Bước 5 - Dùng Integer.MAX_VALUE lấy max và Integer.MIN_VALUE lấy min (logic ngược: sortedlist.get(0) là min, get(size-1) là max) 
Bước 6 - Hiển thị kết quả 
Bước 7 - DỪNG

Ví dụ 1

Gom thao tác dưới '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 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 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)); 
    } 
}

Kết quả

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

Ví dụ 2

Đóng gói thao tác vào hàm thể hiện lập trình hướng đối tượng.

import java.util.ArrayList; 
import java.util.Collections; 
import java.util.List; 

public class Demo { 
    public static Integer get_min_value(List sortedlist) { 
        if (sortedlist == null || sortedlist.size() == 0) { 
            return Integer.MAX_VALUE; 
        } 
        return sortedlist.get(0); 
    } 
    
    public static Integer get_max_value(List 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 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 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)); 
    } 
}

Kết quả

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