[JavaSE] array (get the most value), get the javase Array

Source: Internet
Author: User

[JavaSE] array (get the most value), get the javase Array

Common Operations on arrays (obtain the most value)

1. Compare to obtain the maximum value. Each comparison has a large value. Because the value is not sure, it is stored using a variable.

2. Compare each element in the array with the value in this variable. If it is greater than the value in the variable, use this variable to record the greater

3. When all elements are compared, the maximum value in the array is stored in the variable.

 

The initialization variable is the first element.

The initialization variable is an index, which can obtain the script of the maximum or maximum value.

Java version:

Public class ArrayDemo {/*** @ param args */public static void main (String [] args) {int [] arr = new int [] {2, 5, 4, 1 }; system. out. println (getMax (arr); // output 5 System. out. println (getMax2 (arr); // output 5}/*** get the maximum value * @ param arr * @ return */public static int getMax (int [] arr) {int max = arr [0]; for (int x = 1; x <arr. length; x ++) {if (arr [x]> max) {max = arr [x] ;}} return max ;}/ *** get the maximum value, this can be used to obtain the script mark * @ param arr * @ return */public static int getMax2 (int [] arr) {int index = 0; for (int x = 0; x <arr. length; x ++) {if (arr [x]> arr [index]) {index = x ;}} return arr [index] ;}}

PHP version:

<? Phpclass ArrayDemo {public static function main () {$ arr = array (,); echo ArrayDemo: getMax ($ arr); // output 5 echo ArrayDemo :: getMax2 ($ arr); // output 5}/*** get the maximum value * @ param arr * @ return */public static function getMax ($ arr) {$ max = $ arr [0]; for ($ I = 1; $ I <count ($ arr); $ I ++) {if ($ arr [$ I]> $ max) {$ max = $ arr [$ I] ;}return $ max ;}/ *** get the maximum value, this can be used to obtain the maximum or maximum value of the script * @ param arr * @ return */public static function getMax2 ($ arr) {$ index = 0; for ($ I = 0; $ I <count ($ arr); $ I ++) {if ($ arr [$ I]> $ arr [$ index]) {$ index = $ I ;}} return $ arr [$ index] ;}} ArrayDemo: main ();

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.