Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Java find the maximum value in an array

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "Java how to 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 "Java how to find the maximum value in the array" article.

Method 1: circular comparison

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 value System.out.println ("maximum 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 value for (int item: arr) {if (item > max) {/ / the current value is greater than the maximum value, 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 to recursively call the comparison, until the end of the recursion, you can return the maximum value.

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 System.out.println based on Collections ("maximum is:" + max) } / * according to the maximum value of the recursive query * @ param arr the array to be queried * @ the subscript of the foremost element * @ param last the last subscript of the element * @ param max (temporary) maximum * @ return maximum * / private static int findMaxByRecursive (int [] arr, int head, int last) Int max) {if (head = = last) {/ / Recursive Return the result return max } else {if (arr [head] > arr [last]) {max = arr [head]; / / assign maximum / / move recursive return findMaxByRecursive (arr, head, last-1, max) from back to front;} else {max = arr [last] / / assign maximum / / move recursive return findMaxByRecursive (arr, head + 1, last, max) from after going;}

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 System.out.println ("maximum is:" + max) based on Arrays.sort } / * find the maximum value according to 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 System.out.println ("maximum is:" + max) based on stream } / * find the maximum value according to 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 System.out.println ("maximum is:" + max) based on Collections } / * find the maximum value according to 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 (break;-= = left) {break;} a [j + 1] = ai } the above is the content of this article on "how to find the maximum value in the array by Java". I believe you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to learn 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report