maximum value in two dimensional array - Java Collection Framework

Java examples for Collection Framework:Array Algorithm

Description

maximum value in two dimensional array

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        double[] arr = new double[] { 34.45, 35.45, 36.67, 37.78, 37.0000,
                37.1234, 67.2344, 68.34534, 69.87700 };
        System.out.println(maximum(arr));
    }//from w w  w  . j a  v a 2s . c om

    public static double maximum(double[] arr) {
        double maxVal = Double.NEGATIVE_INFINITY;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > maxVal) {
                maxVal = arr[i];
            }
        }
        return maxVal;
    }

    public static double maximum(double[][] arr) {
        double maxVal = Double.NEGATIVE_INFINITY;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null) {
                maxVal = Math.max(maxVal, maximum(arr[i]));
            }
        }
        return maxVal;
    }

    public static double maximum(double[][][] arr) {
        double maxVal = Double.NEGATIVE_INFINITY;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null) {
                maxVal = Math.max(maxVal, maximum(arr[i]));
            }
        }
        return maxVal;
    }
}

Related Tutorials