In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article, "java find the maximum value in the array", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this article, "what are the maximum values in the java search array?"
Method 1: circular comparison
The execution flow of the loop comparison is shown in the following figure:
As you can see from the above figure, the core of circular comparison is to define a maximum value, and then cycle through each element. If the value of the element is greater than the maximum value, update the maximum value to the value of this element, and then make the next comparison. We can find the maximum value until the end of the loop. The implementation code is as follows:
Public class ArrayMaxTest {
Public static void main (String [] args) {
Int [] arr = {3,7,2,1,-4}
Int max = findMaxByFor (arr); / / find the maximum
System.out.println ("Max is:" + max)
}
/ * *
* find the maximum value through the for loop
* @ param arr Array to be queried
* @ return maximum
, /
Private static int findMaxByFor (int [] arr) {
Int max = 0; / / maximum
For (int item: arr) {
If (item > max) {/ / the current value is greater than the maximum value and is assigned to the maximum value
Max = item
}
}
Return max
}
}
The results of the above procedures are as follows:
The maximum is: 7
Method 2: recursive comparison
The core of recursive comparison is to define two positions (the start position and the end position), compare the values of the start position and the end position each time, and set the maximum value to the value of the start position when the value of the start position is greater than the value of the end position. then move the end position-1 (move forward one bit) and continue to recursively call On the contrary, when the value of the end position is greater than that of the start position, set the maximum value to the value of the end position, move the start position + 1 (move one bit back), and continue the recursive call comparison, until the end of the recursion, you can return the maximum value. The execution process is as follows:
The implementation code is as follows:
Public class ArrayMax {
Public static void main (String [] args) {
Int [] arr = {3,7,2,1,-4}
Int max = findMaxByRecursive (arr, 0, arr.length-1,0); / / find the maximum value based on Collections
System.out.println ("Max is:" + max)
}
/ * *
* query the maximum value according to recursion
* @ param arr Array to be queried
* @ param head the subscript of the foremost element
The subscript of the last element of * @ param last
* @ param max (temporary) maximum
* @ return maximum
, /
Private static int findMaxByRecursive (int [] arr, int head, int last, int max) {
If (head = = last) {
/ / Recursion is finished, and the result is returned.
Return max
} else {
If (arr [head] > arr [last]) {
Max = arr [head]; / / assign the maximum value
/ / move recursion from back to front
Return findMaxByRecursive (arr, head, last-1, max)
} else {
Max = arr [last]; / / assign the maximum value
/ / move recursion from the back to the back
Return findMaxByRecursive (arr, head + 1, last, max)
}
}
}
}
The results of the above procedures are as follows:
The maximum is: 7
Method 3: rely on Arrays.sort () to implement
The array can be sorted from small to large according to the Arrays.sort method. After the sorting is completed, the value of the last bit is the maximum value. The implementation code is as follows:
Import java.util.Arrays
Public class ArrayMax {
Public static void main (String [] args) {
Int [] arr = {3,7,2,1,-4}
Int max = findMaxBySort (arr); / / find the maximum value based on Arrays.sort
System.out.println ("Max is:" + max)
}
/ * *
* find the maximum value based on Arrays.sort
* @ param arr Array to be queried
* @ return maximum
, /
Private static int findMaxBySort (int [] arr) {
Arrays.sort (arr)
Return arr [arr.length-1]
}
}
The results of the above procedures are as follows:
The maximum is: 7
Method 4: implement according to Arrays.stream ()
Stream is one of the new core features of JDK 8. Using it, we can easily implement many functions, such as finding the maximum and minimum values. The implementation code is as follows:
Import java.util.Arrays
Public class ArrayMax {
Public static void main (String [] args) {
Int [] arr = {3,7,2,1,-4}
Int max = findMaxByStream (arr); / / find the maximum value based on stream
System.out.println ("Max is:" + max)
}
/ * *
* find the maximum value based on stream
* @ param arr Array to be queried
* @ return maximum
, /
Private static int findMaxByStream (int [] arr) {
Return Arrays.stream (arr). Max (). GetAsInt ()
}
}
The results of the above procedures are as follows:
The maximum is: 7
Method 5: rely on Collections.max () to implement
You can also find the maximum and minimum values using the Collections collection utility class, but before using it, we want to convert an array (Array) to a collection (List), as follows:
Import org.apache.commons.lang3.ArrayUtils
Import java.util.Arrays
Import java.util.Collections
Public class ArrayMax {
Public static void main (String [] args) {
Int [] arr = {3,7,2,1,-4}
Int max = findMaxByCollections (arr); / / find the maximum value based on Collections
System.out.println ("Max is:" + max)
}
/ * *
* find the maximum value based on Collections
* @ param arr Array to be queried
* @ return maximum
, /
Private static int findMaxByCollections (int [] arr) {
List list = Arrays.asList (
Org.apache.commons.lang3.ArrayUtils.toObject (arr))
Return Collections.max (list)
}
}
The results of the above procedures are as follows:
The maximum is: 7
Extended knowledge: the principle of Arrays.sort method execution
In order to understand the principle of Arrays#sort method execution, we looked at the source code to discover that the core of the sort method is sorted through a loop. The source code is as follows:
For (int I = left, j = I; I < right; j = + + I) {
Int ai = a [I + 1]
While (ai < a [j]) {
A [j + 1] = a [j]
If (jmurt-= = left) {
Break
}
}
A [j + 1] = ai
}
The execution process is shown in the following figure:
The above is about the content of this article on "what are the methods for java to find the maximum values in the array". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.