Java Array Equal equalsToElement(final Object[] arr, final Object obj)

Here you can find the source of equalsToElement(final Object[] arr, final Object obj)

Description

Checks if at least one array element equals the passed parameter.

License

Open Source License

Parameter

Parameter Description
arr the array
obj the element to compare

Return

true, if at least one array element equals to the passed parameter, otherwise false

Declaration

public static boolean equalsToElement(final Object[] arr,
        final Object obj) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2004-2013//from w w w . j ava  2  s.co  m
 * Contributors: L. Armanet, M. Camerlenghi, L. Cardonne, S. Delageniere,
 *               L. Duparchy, S. Ohlsson, P. Pascal, I. Schneider, S.Schulze,
 *               F. Torres
 * 
 * This file is part of the MIS tools package.
 * 
 * The MIS tools package is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * The MIS tools package 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 Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with the MIS tools package.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

import java.util.List;

public class Main {
    /**
     * Checks if at least one array element equals the passed parameter.
     * 
     * @param arr
     *            the array
     * @param obj
     *            the element to compare
     * @return true, if at least one array element equals to the passed parameter, otherwise false
     */
    public static boolean equalsToElement(final Object[] arr,
            final Object obj) {
        if (arr == null) {
            if (obj == null) {
                return true;
            }
            return false;
        }
        if (obj == null) {
            return false;
        }
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] instanceof String && obj instanceof String) {
                if (((String) arr[i]).equalsIgnoreCase((String) obj)) {
                    return true;
                }
            } else if (arr[i].equals(obj)) {
                return true;
            }
        }
        return false;
    }

    /**
     * Checks if at least one array element equals the passed parameter.
     * 
     * @param arr
     *            the array
     * @param obj
     *            the element to compare
     * @return true, if at least one array element equals to the passed parameter, otherwise false
     */
    public static <T> boolean equalsToElement(final List<T> arr, final T obj) {
        if (arr == null) {
            if (obj == null) {
                return true;
            }
            return false;
        }
        if (obj == null) {
            return false;
        }
        for (int i = 0; i < arr.size(); i++) {
            if (arr.get(i) instanceof String && obj instanceof String) {
                if (((String) arr.get(i)).equalsIgnoreCase((String) obj)) {
                    return true;
                }
            } else if (arr.get(i).equals(obj)) {
                return true;
            }
        }
        return false;
    }
}

Related

  1. equals(T[] array1, T[] array2)
  2. equals3(double[][][] arr1, double[][][] arr2)
  3. equalsAny(T element, T[] array)
  4. equalsIgnoreSequence(final T[] array1, final T[] array2)
  5. equalsIgnoreSequence(Object[] array1, Object[] array2)
  6. equalStringArrays(String[] expected, String[] actual)
  7. findMinimaGreaterOrEqual(final int[] min, final int elt)
  8. getClosestIndexEqualToOrLargerThanGivenCoordinate(double[] coordinates, double coordinate)
  9. isEquals(byte[] id1, byte[] id2)