Computer >> Máy Tính >  >> Lập trình >> Java

Chương trình Java để nhận tối thiểu và tối đa từ một danh sách

Trong bài viết này, chúng ta sẽ hiểu cách lấy tối thiểu và tối đa từ một danh sách. Danh sách là một tập hợp có thứ tự cho phép chúng ta lưu trữ và truy cập các phần tử một cách tuần tự. Nó chứa các phương thức dựa trên chỉ mục để chèn, cập nhật, xóa và tìm kiếm các phần tử. Nó cũng có thể có các phần tử trùng lặp.

Dưới đây là một minh chứng về điều tương tự -

Giả sử đầu vào của chúng tôi là -

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

Đầu ra mong muốn sẽ là -

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

Thuật toán

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

Ví dụ 1

Ở đây, chúng tôi liên kết tất cả các hoạt động với nhau trong hàm "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));
   }
}

Đầu ra

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

Ở đây, chúng tôi đóng gói các hoạt động thành các 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<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));
   }
}

Đầu ra

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