Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Convenience methods for working with Java arrays.
 * Copyright (C) 2005-2010 Stephen Ostermiller
 * http://ostermiller.org/contact.pl?regarding=Java+Utilities
 *
 * 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.
 *
 * See LICENSE.txt for details.
 */

public class Main {
    /**
     * Tests two arrays to see if the arrays are equal.
     * Two arrays will be equal only if they are the same length
     * and contain objects that are equal in the same order.
     *
     * @param arr1 first array
     * @param arr2 second array
     * @return true iff two arguments are equal
     * @since ostermillerutils 1.06.00
     */
    public static boolean equal(Object[] arr1, Object[] arr2) {
        if (arr1 == null && arr2 == null)
            return true;
        if (arr1 == null || arr2 == null)
            return false;
        if (arr1.length != arr2.length)
            return false;
        for (int i = 0; i < arr1.length; i++) {
            if (!equalObjects(arr1[i], arr2[i]))
                return false;
        }
        return true;
    }

    /**
     * Tests if the two objects are equal
     *
     * @param o1 first object
     * @param o2 second object
     * @since ostermillerutils 1.06.00
     */
    private static boolean equalObjects(Object o1, Object o2) {
        if (o1 == null && o2 == null)
            return true;
        if (o1 == null || o2 == null)
            return false;
        return o1.equals(o2);
    }
}