Determines if the two arrays are equal Two arrays are equal if their size is identical and if for each position in the array, both corresponding elements are equal - Java Collection Framework

Java examples for Collection Framework:Array Equal

Description

Determines if the two arrays are equal Two arrays are equal if their size is identical and if for each position in the array, both corresponding elements are equal

Demo Code

/*//w  w  w.j a  va  2 s  .c o m
    Copyright 1996-2008 Ariba, Inc.

    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.

    $Id: //ariba/platform/util/core/ariba/util/core/ArrayUtil.java#9 $
 */
import java.io.PrintStream;
import java.util.List;

public class Main{
    public static void main(String[] argv) throws Exception{
        Object[] a1 = new String[]{"1","abc","level",null,"java2s.com","asdf 123"};
        Object[] a2 = new String[]{"1","abc","level",null,"java2s.com","asdf 123"};
        System.out.println(arrayEquals(a1,a2));
    }
    /**
        Determines if the two arrays are equal
        Two arrays are equal if their size is identical and
        if for each position in the array, both corresponding
        elements are equal

        @param a1 first array
        @param a2 second array
        @return <code>true</code> if the two arrays are equal
        @aribaapi documented
     */
    public static boolean arrayEquals(Object[] a1, Object[] a2) {
        if (a1 == null && a2 == null) {
            return true;
        }
        if (a1 == null || a2 == null) {
            return false;
        }
        if (a1.length != a2.length) {
            return false;
        }
        for (int e = 0, l = a1.length; e < l; e++) {
            if (!SystemUtil.objectEquals(a1[e], a2[e])) {
                return false;
            }
        }
        return true;
    }
}

Related Tutorials