Java Array Intersect arraysIntersect(Object[] array1, Object[] array2)

Here you can find the source of arraysIntersect(Object[] array1, Object[] array2)

Description

Check if two arrays have at least one common element.

License

Open Source License

Parameter

Parameter Description
array1 first object array
array2 second object array

Return

Returns true if the arrays have one or more common elements

Declaration

public static boolean arraysIntersect(Object[] array1, Object[] array2) 

Method Source Code

//package com.java2s;
/**//from   w ww  .j a  va  2  s  .co  m
 * Copyright (C) 2014 WING, NUS and NUS NLP Group.
 * 
 * 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 3 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, see http://www.gnu.org/licenses/.
 */

public class Main {
    /**
     * Check if two arrays have at least one common element.
     * 
     * @param array1 first object array
     * @param array2 second object array
     * @return Returns {@code true} if the arrays have one or more common elements
     */
    public static boolean arraysIntersect(Object[] array1, Object[] array2) {

        boolean intersect = false;
        for (int i = 0; i < array1.length && !intersect; ++i) {

            Object e1 = array1[i];
            if (e1 != null) {
                for (int j = 0; j < array2.length && !intersect; ++j) {
                    Object e2 = array2[j];
                    intersect = e1.equals(e2);
                }
            }
        }

        return intersect;
    }
}

Related

  1. arrayInterp(double[] inputArray, double index)
  2. byteIntersection(byte[] a, byte[] b)
  3. getArrayIntersection(int a[], int b[])
  4. getNonIntersection(int[] interval, int[] intervalToRemove)
  5. hasIntersection(String a1[], String a2[], int mode)