Here you can find the source of maxIndex(final double[] values)
double
and returns its index in the array.
Parameter | Description |
---|---|
values | an array of double. |
public static int maxIndex(final double[] values)
//package com.java2s; /*//w w w . j a v a2s. c o m * Copyright 2007-2010 Tom Castle & Lawrence Beadle * Licensed under GNU General Public License * * This file is part of EpochX: genetic programming software for research * * EpochX 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. * * EpochX 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 EpochX. If not, see <http://www.gnu.org/licenses/>. * * The latest version is available from: http:/www.epochx.org */ public class Main { /** * Finds the maximum value of an array of <code>double</code> and returns * its index in the array. The provided argument must not be null nor an * empty array. * * @param values an array of double. * @return the index of the maximum value in the provided array of doubles. */ public static int maxIndex(final double[] values) { if ((values != null) && (values.length != 0)) { double max = 0; int maxIndex = -1; for (int i = 0; i < values.length; i++) { if (values[i] > max) { max = values[i]; maxIndex = i; } } return maxIndex; } else { throw new IllegalArgumentException("cannot calculate maximum index of null or empty array of values"); } } /** * Finds the maximum value of an array of <code>int</code> and returns * its index in the array. The provided argument must not be null nor an * empty array. * * @param values an array of int. * @return the index of the maximum value in the provided array of ints. */ public static int maxIndex(final int[] values) { if ((values != null) && (values.length != 0)) { int max = 0; int maxIndex = -1; for (int i = 0; i < values.length; i++) { if (values[i] > max) { max = values[i]; maxIndex = i; } } return maxIndex; } else { throw new IllegalArgumentException("cannot calculate maximum index of null or empty array of values"); } } }