Java Array Max Value maxIndex(double[] a)

Here you can find the source of maxIndex(double[] a)

Description

Returns the index of the maximum value in array a.

License

Apache License

Parameter

Parameter Description
a a parameter

Return

index of maximum value or -1 if a.length==0 or a==null

Declaration

public static final int maxIndex(double[] a) 

Method Source Code

//package com.java2s;
/*/*from  www. ja  v  a 2 s  .  com*/
 * Copyright 2014 Jon N. Marsh.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * Returns the index of the maximum value in array a. If {@code a==null} or
     * {@code a.length==0}, the return value is {@code -1}. If several elements
     * have the same value equal to the maximum value of {@code a}, the returned
     * value is the index of the first instance of the maximum value.
     *
     * @param a
     * @return index of maximum value or {@code -1} if {@code a.length==0} or
     *         {@code a==null}
     */
    public static final int maxIndex(double[] a) {
        int index = -1;

        if (a != null && a.length != 0) {
            double max = a[0];
            index = 0;
            for (int i = 1; i < a.length; i++) {
                if (a[i] > max) {
                    max = a[i];
                    index = i;
                }
            }
        }

        return index;
    }

    /**
     * Returns the index of the maximum value in array a. If {@code a==null} or
     * {@code a.length==0}, the return value is {@code -1}. If several elements
     * have the same value equal to the maximum value of {@code a}, the returned
     * value is the index of the first instance of the maximum value.
     *
     * @param a
     * @return index of maximum value or {@code -1} if {@code a.length==0} or
     *         {@code a==null}
     */
    public static final int maxIndex(float[] a) {
        int index = -1;

        if (a != null && a.length != 0) {
            float max = a[0];
            index = 0;
            for (int i = 1; i < a.length; i++) {
                if (a[i] > max) {
                    max = a[i];
                    index = i;
                }
            }
        }

        return index;
    }
}

Related

  1. maxIn(double[] array)
  2. maxInArray(int[] src)
  3. maxInArray(int[] values)
  4. maxIndAbs(double[] a)
  5. maxIndex(double values[])
  6. maxIndex(double[] arr)
  7. maxIndex(double[] arr)
  8. maxIndex(double[] array)
  9. maxIndex(double[] array)