Java Array Min Value minIndex(double[] a)

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

Description

Returns the index of the minimum value in array a.

License

Apache License

Parameter

Parameter Description
a a parameter

Return

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

Declaration

public static final int minIndex(double[] a) 

Method Source Code

//package com.java2s;
/*/* w  w w .  j av  a2  s  .co m*/
 * 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 minimum 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 minimum value of {@code a}, the returned
     * value is the index of the first instance of the minimum value.
     *
     * @param a
     * @return index of minimum value or {@code -1} if {@code a.length==0} or
     *         {@code a==null}
     */
    public static final int minIndex(double[] a) {
        int index = -1;

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

        return index;
    }

    /**
     * Returns the index of the minimum 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 minimum value of {@code a}, the returned
     * value is the index of the first instance of the minimum value.
     *
     * @param a
     * @return index of minimum value or {@code -1} if {@code a.length==0} or
     *         {@code a==null}
     */
    public static final int minIndex(float[] a) {
        int index = -1;

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

        return index;
    }
}

Related

  1. minimumOf(final int[] array)
  2. minimumWordLength(String[] words)
  3. minIn(double[] array)
  4. minIndex(double values[])
  5. minIndex(double... data)
  6. minIndex(double[] arr)
  7. minIndex(double[] list)
  8. minIndex(double[] v)
  9. minIndex(double[] value)