search
HomeJavaJavagetting StartedHow to implement the minimum value in an array in java

How to implement the minimum value in an array in java

Purpose:

First create an array with a length of 5, then assign a random integer to each bit of the array, and find the smallest (largest) value come out.

Method 1:

        int array[] = new int[5];
        System.out.println("数组的元素为:");
        for (int i=0;i<array.length;i++){
            array[i] = (int) (Math.random()*100);
            System.out.println(array[i]);
        }
        System.out.println("----------------------------------------------------");
        int min = array[0];
        for(int i=1;i<array.length;i++)
        {
            if(min>array[i]){
                min=array[i];
            }

        }
        System.out.println("方法二:最小值为:"+min);
    }

Result:

How to implement the minimum value in an array in java

## (Recommended tutorial:

java quick start)

Method 2:

        int array[] = new int[5];
        System.out.println("数组的元素为:");
        for (int i=0;i<array.length;i++){
            array[i] = (int) (Math.random()*100);
            System.out.println(array[i]);
        }
        System.out.println("----------------------------------------------------");
        //对数组进行排序处理
        Arrays.sort(array);
        System.out.println("方法三:最小值为:"+array[0]);
    }

Result:


How to implement the minimum value in an array in java##Method 3:

Collections through Collections class .max() and Collections.min() methods to find the maximum and minimum values ​​in an array.

The code is as follows:

        Integer array[] = new Integer[5];
        System.out.println("数组的元素为:");
        for (int i=0;i<array.length;i++){
            array[i] = (int) (Math.random()*100);
            System.out.println(array[i]);
        }
        System.out.println("----------------------------------------------------");
        //通过 Collections 类的 Collections.max() 和 Collections.min() 方法来查找数组中的最大和最小值:
        int min = (int)Collections.min(Arrays.asList(array));
        int max = (int) Collections.max(Arrays.asList(array));
        System.out.println("方法四:最小值为:"+min);
        System.out.println("方法四:最大值为:"+max);

Result:


## Recommended related video tutorials: How to implement the minimum value in an array in javajava video tutorial

The above is the detailed content of How to implement the minimum value in an array in java. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:csdn. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use