Java Array Index Of indexOf(int[] arr, int val)

Here you can find the source of indexOf(int[] arr, int val)

Description

index Of

License

Open Source License

Declaration

public static int indexOf(int[] arr, int val) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;

import java.util.Set;

public class Main {
    public static int indexOf(int[] arr, int val) {
        if (arr == null)
            return -1;
        for (int i = 0; i < arr.length; i++)
            if (arr[i] == val)
                return i;
        return -1;
    }//from   w  w  w . j a  v a 2 s  .com

    public static int indexOf(Object[] objs, Object obj) {
        for (int i = 0; i < objs.length; i++)
            if ((objs[i] == obj) || ((objs[i] != null) && objs[i].equals(obj)))
                return i;
        return -1;
    }

    public static boolean equals(Object[] objs1, Object[] objs2) {
        if ((objs1 == null) && (objs2 == null))
            return true;
        if ((objs1 == null) || (objs2 == null))
            return false;
        if (objs1.length != objs2.length)
            return false;
        Set<Object> set1 = toSet(objs1);
        Set<Object> set2 = toSet(objs2);
        set1.removeAll(set2);
        return set1.size() == 0;
    }

    /**
     * @param selection
     * @return
     */
    public static Set<Object> toSet(Object[] objs) {
        Set<Object> ret = new HashSet<Object>();
        if (objs != null)
            for (int i = 0; i < objs.length; i++)
                ret.add(objs[i]);
        return ret;
    }

    public static void removeAll(Collection<Object> ret, Object[] elements) {
        if (elements != null)
            for (int i = 0; i < elements.length; i++)
                ret.remove(elements[i]);
    }

    public static void removeAll(Collection<Object> ret, Iterator<Object> elements) {
        if (elements != null)
            while (elements.hasNext())
                ret.remove(elements.next());
    }
}

Related

  1. indexOf(byte[] target, byte[] key)
  2. indexOf(char toBeFound, char[] array)
  3. indexOf(final String[] p_elements, final String p_key)
  4. indexOf(final T[] array, final T value)
  5. indexOf(int[] arr, int e)
  6. indexOf(Object[] array, Object objectToFind)
  7. indexOf(Object[] array, Object objectToFind)
  8. indexOf(Object[] elements, Object value)
  9. indexOfCS(String[] strings, String a)