Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the ideas and methods for obtaining the maximum value of an array?

Shulou Source: shulou.com Published: 2022-06-02 10:28:26 09月27日 Update

This article will explain in detail what are the ideas and methods to obtain the maximum value of the array. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Analysis of ideas:

1. Define a variable (max, the initial value is usually the first element value in the array) to record the maximum value.

2. Traverse the array, get each element in the array, and then compare it with max in turn. If the element currently traversed is larger than max, the value of the current element is given to max. If small, do not operate.

3. At the end of the loop, max records the maximum value.

Specific code

Public static void main (String [] args) {

Int [] arr = {11, 22, 33, 44, 55}

GetMax (arr)

GetMin (arr)

}

Private static void getMin (int [] arr) {

Int min = arr [0]

For (int I = 0; I

< arr.length; i++) { if(min>

Arr [I]) {

Min=arr [i]

}

}

System.out.println ("min=" + min)

}

Private static void getMax (int [] arr) {

Int max = arr [0]

For (int I = 0; I < arr.length; iTunes +) {

If (max

Tags: Arrays elements ideas methods maximum content articles more maximum knowledge articles good code variables that is quality analysis reference help loop Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn MySQL macOS Xiaomi Linux