1Array -- Find Maximum
2Write a method that can find the maximum number from an int Array
3Solution 1:
4public static void main(String[] args) {
5 int[] arr = new int[]{2,4,6,8,20};
6 System.out.println(maxValue(arr));
7
8public static int maxValue( int[] n ) {
9int max = Integer.MIN_VALUE;
10for(int each: n)
11if(each > max)
12max = each;
13
14return max;
15}
16
17Solution 2:
18public static int maxValue( int[] n ) {
19Arrays.sort( n );
20return n [ n.lenth-1 ];
21}
22
1function getMaxOfArray(numArray) {
2 return Math.max.apply(null, numArray);
3}
1---without sort method---
2public static int maxValue( int[] n ) {
3
4int max = Integer.MIN_VALUE;
5
6for(int each: n)
7
8if(each > max)
9
10max = each;
11
12
13return max;
14
15---with sort method---
16public static int maxValue( int[] n ) {
17
18Arrays.sort( n );
19
20return n [ n.lenth-1 ];
21
22}