Java Array Max Value max(double[] array)

Here you can find the source of max(double[] array)

Description

max

License

Open Source License

Declaration

public static double max(double[] array) 

Method Source Code

//package com.java2s;
/*//from  w  ww . ja v a  2s.c o  m
 *  Java Information Dynamics Toolkit (JIDT)
 *  Copyright (C) 2012, Joseph T. Lizier
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    public static double max(double[][][] matrix) {
        // double max = 0.0;
        double max = matrix[0][0][0];
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                for (int k = 0; k < matrix[i][j].length; k++) {
                    if (matrix[i][j][k] > max) {
                        max = matrix[i][j][k];
                    }
                }
            }
        }
        return max;
    }

    public static double max(double[][] matrix) {
        // double max = 0.0;
        double max = matrix[0][0];
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                if (Double.isNaN(max) || (matrix[i][j] > max)) {
                    max = matrix[i][j];
                }
            }
        }
        return max;
    }

    public static int max(int[][] matrix) {
        // int max = 0;
        int max = matrix[0][0];
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                if (matrix[i][j] > max) {
                    max = matrix[i][j];
                }
            }
        }
        return max;
    }

    public static double max(double[] array) {
        return maxStartFromIndex(array, 0);
    }

    public static int max(int[] array) {
        // int max = 0;
        int max = array[0];
        for (int i = 0; i < array.length; i++) {
            if (array[i] > max) {
                max = array[i];
            }
        }
        return max;
    }

    /**
     * Works out the maximum value in the matrix in a given column 
     * 
     * @param matrix
     * @param column
     * @return
     */
    public static double max(double[][] matrix, int column) {
        // double max = 0.0;
        // Allow ArrayIndexOutOfBoundsException if matrix is size 0
        double max = matrix[0][column];
        for (int i = 1; i < matrix.length; i++) {
            if (Double.isNaN(max) || (matrix[i][column] > max)) {
                max = matrix[i][column];
            }
        }
        return max;
    }

    /**
     * Works out the maximum value in the matrix in a given column 
     * 
     * @param matrix
     * @param column
     * @return
     */
    public static int max(int[][] matrix, int column) {
        // double max = 0.0;
        // Allow ArrayIndexOutOfBoundsException if matrix is size 0
        int max = matrix[0][column];
        for (int i = 1; i < matrix.length; i++) {
            if (matrix[i][column] > max) {
                max = matrix[i][column];
            }
        }
        return max;
    }

    public static double maxStartFromIndex(double[] array, int startFromIndex) {
        // double max = 0.0;
        double max = array[startFromIndex];
        for (int i = startFromIndex; i < array.length; i++) {
            // TODO Check where we used this and if it's still
            //  the approach we want to take
            if (Double.isNaN(max) || (array[i] > max)) {
                max = array[i];
            }
        }
        return max;
    }
}

Related

  1. max(double[] array)
  2. max(double[] array)
  3. max(double[] array)
  4. max(double[] array)
  5. max(double[] array)
  6. max(double[] data)
  7. max(double[] data)
  8. max(double[] list)
  9. max(double[] matrix)