What are the ideas and methods for obtaining the maximum value of an array?
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