Java Array Equal arraysEqual(String[] arr1, String[] arr2)

Here you can find the source of arraysEqual(String[] arr1, String[] arr2)

Description

Match two arrays for equality

License

Apache License

Parameter

Parameter Description
arr1 a parameter
arr2 a parameter

Declaration

public static boolean arraysEqual(String[] arr1, String[] arr2) 

Method Source Code

//package com.java2s;
/*//from w ww  .  j a  va 2s .c o m
 * JBoss, Home of Professional Open Source
 *
 * Copyright 2013 Red Hat, Inc. and/or its affiliates.
 *
 * 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 {
    /**
     * Match two arrays for equality
     *
     * @param arr1
     * @param arr2
     * @return
     */
    public static boolean arraysEqual(String[] arr1, String[] arr2) {
        if (arr1 != null && arr2 == null) {
            return false;
        }
        if (arr1 == null && arr2 == null) {
            return true;
        }
        if (arr1 == null && arr2 != null) {
            return false;
        }

        int length1 = arr1.length;
        int length2 = arr2.length;
        if (length1 != length2) {
            return false;
        }
        boolean foundMatch = false;
        for (int i = 0; i < length1; i++) {
            for (int j = 0; j < length2; j++) {
                if (arr1[i].equals(arr2[j])) {
                    foundMatch = true;
                    break;
                }
            }
            if (foundMatch == false) {
                return false;
            }
            // reset
            foundMatch = false;
        }
        return true;
    }
}

Related

  1. arraysEqual(boolean[] a, boolean[] b)
  2. arraysEqual(byte array1[], byte array2[])
  3. arraysEqual(byte[] bytes, byte[] ints)
  4. arraysEqual(final Object[] a1, final Object[] a2)
  5. arraysEqual(Object[] array1, Object[] array2)
  6. arraysEqual(String[] one, String[] other)
  7. arraysEquals(byte[] a, byte[] b)
  8. arraysEquals(byte[] a, int ofsA, byte[] b, int ofsB, int len)
  9. arraysEquals(byte[] arr1, int offset1, byte[] arr2, int offset2)