Java Array Equal allEqual(int[] values)

Here you can find the source of allEqual(int[] values)

Description

Checks if elements in the given array are all equal.

License

Open Source License

Parameter

Parameter Description
values the array

Return

true if all elements in the given array are equal

Declaration

public static boolean allEqual(int[] values) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*from  ww  w.j ava2s .co  m*/
     * Checks if elements in the given array are all equal.
     * @param values the array
     * @return true if all elements in the given array are equal
     */
    public static boolean allEqual(int[] values) {
        if (values.length == 0) {
            return true;
        }
        int value = values[0];
        for (int i = 1; i < values.length; i++) {
            if (values[i] != value) {
                return false;
            }
        }
        return true;
    }

    /**
     * Checks if elements in the given array are all equal.
     * @param values the array
     * @return true if all elements in the given array are equal
     */
    public static boolean allEqual(double[] values) {
        if (values.length == 0) {
            return true;
        }
        double value = values[0];
        for (int i = 1; i < values.length; i++) {
            if (values[i] != value) {
                return false;
            }
        }
        return true;
    }

    /**
     * Checks if elements in the given array are all equal.
     * @param values the array
     * @return true if all elements in the given array are equal
     */
    public static boolean allEqual(boolean[] values) {
        if (values.length == 0) {
            return true;
        }
        boolean value = values[0];
        for (int i = 1; i < values.length; i++) {
            if (values[i] != value) {
                return false;
            }
        }
        return true;
    }
}

Related

  1. allEqual(int[] arr)
  2. allEquals(double[][] sims)
  3. allEquals(final String value, final String... strings)
  4. allEquals(int needle, byte... bytes)
  5. allEquals(int y, int y2, int y3)