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

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

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

Share

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

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

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