[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 ();