Java Array Equal arrayEquals(int[] a1, int[] a2)

Here you can find the source of arrayEquals(int[] a1, int[] a2)

Description

Checks whether the two passed in arrays are equal.

License

Open Source License

Parameter

Parameter Description
a1 the first array
a2 the second array

Return

true iff the objects are equal

Declaration

public static boolean arrayEquals(int[] a1, int[] a2) 

Method Source Code

//package com.java2s;
/*//  w  w  w. j a v  a  2  s  . com
 * $Id: CountingSemaphore.java 914 2005-11-03 08:42:52 +0100 (Thu, 03 Nov 2005) sim $
 *
 * ace - a collaborative editor
 * Copyright (C) 2005 Mark Bigler, Simon Raess, Lukas Zbinden
 *
 * 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 2
 * 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

public class Main {
    /**
     * Checks whether the two passed in arrays are equal. To be equal, both
     * arrays must have the same length and contain the same value at
     * the same index.
     * 
     * @param a1 the first array
     * @param a2 the second array
     * @return true iff the objects are equal
     */
    public static boolean arrayEquals(int[] a1, int[] a2) {
        if (a1 == a2) {
            return true;
        } else if (a1 == null || a2 == null) {
            return false;
        } else if (a1.length != a2.length) {
            return false;
        } else {
            for (int i = 0; i < a1.length; i++) {
                if (a1[i] != a2[i]) {
                    return false;
                }
            }
            return true;
        }
    }
}

Related

  1. areEqualArrays(Object[] a1, Object[] a2)
  2. arrayEq(byte[] a, byte[] b)
  3. arrayEqual(char[] a, char[] b, int aStart, int bStart, int length)
  4. arrayEquals(byte[] data, byte[] expected)
  5. arrayEquals(byte[] left, byte[] right)
  6. arrayEquals(int[] arr1, int[] arr2)
  7. arrayEquals(Object[] a, Object[] b)
  8. arrayEquals(Object[] a, Object[] b)
  9. arrayEquals(Object[] arr1, Object[] arr2)