Java Array Remove removeAll(E[] array, Object... toRemove)

Here you can find the source of removeAll(E[] array, Object... toRemove)

Description

remove All

License

Open Source License

Declaration

public static <E> E[] removeAll(E[] array, Object... toRemove) 

Method Source Code

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

import java.util.Arrays;

public class Main {
    public static <E> E[] removeAll(E[] array, Object... toRemove) {
        final E[] removed = Arrays.copyOf(array, array.length - count(array, toRemove));
        int index = 0;

        for (E element : array) {
            if (!contains(toRemove, element)) {
                removed[index++] = element;
            }/*  www .ja  v a 2 s.co  m*/
        }

        return removed;
    }

    public static int count(Object[] array, Object... objects) {
        int count = 0;

        for (Object element : array) {
            for (Object object : objects) {
                if (object == null) {
                    if (element == null) {
                        count++;
                    }

                    continue;
                }

                if (object.equals(element)) {
                    count++;
                }
            }
        }

        return count;
    }

    public static boolean contains(Object[] array, Object object) {
        if (object != null) {
            for (Object element : array) {
                if (object.equals(element)) {
                    return true;
                }
            }

            return false;
        }

        for (Object element : array) {
            if (element == null) {
                return true;
            }
        }

        return false;
    }

    public static boolean contains(byte[] array, byte value) {
        return contains(toBoxedArray(array), value);
    }

    public static boolean contains(char[] array, char value) {
        return contains(toBoxedArray(array), value);
    }

    public static boolean contains(double[] array, double value) {
        return contains(toBoxedArray(array), value);
    }

    public static boolean contains(float[] array, float value) {
        return contains(toBoxedArray(array), value);
    }

    public static boolean contains(int[] array, int value) {
        return contains(toBoxedArray(array), value);
    }

    public static boolean contains(long[] array, long value) {
        return contains(toBoxedArray(array), value);
    }

    public static boolean contains(short[] array, short value) {
        return contains(toBoxedArray(array), value);
    }

    public static Boolean[] toBoxedArray(boolean[] array) {
        final Boolean[] objectArray = new Boolean[array.length];
        for (int i = 0; i < array.length; i++) {
            objectArray[i] = array[i];
        }
        return objectArray;
    }

    public static Byte[] toBoxedArray(byte[] array) {
        final Byte[] objectArray = new Byte[array.length];
        for (int i = 0; i < array.length; i++) {
            objectArray[i] = array[i];
        }
        return objectArray;
    }

    public static Character[] toBoxedArray(char[] array) {
        final Character[] objectArray = new Character[array.length];
        for (int i = 0; i < array.length; i++) {
            objectArray[i] = array[i];
        }
        return objectArray;
    }

    public static Double[] toBoxedArray(double[] array) {
        final Double[] objectArray = new Double[array.length];
        for (int i = 0; i < array.length; i++) {
            objectArray[i] = array[i];
        }
        return objectArray;
    }

    public static Float[] toBoxedArray(float[] array) {
        final Float[] objectArray = new Float[array.length];
        for (int i = 0; i < array.length; i++) {
            objectArray[i] = array[i];
        }
        return objectArray;
    }

    public static Integer[] toBoxedArray(int[] array) {
        final Integer[] objectArray = new Integer[array.length];
        for (int i = 0; i < array.length; i++) {
            objectArray[i] = array[i];
        }
        return objectArray;
    }

    public static Long[] toBoxedArray(long[] array) {
        final Long[] objectArray = new Long[array.length];
        for (int i = 0; i < array.length; i++) {
            objectArray[i] = array[i];
        }
        return objectArray;
    }

    public static Short[] toBoxedArray(short[] array) {
        final Short[] objectArray = new Short[array.length];
        for (int i = 0; i < array.length; i++) {
            objectArray[i] = array[i];
        }
        return objectArray;
    }
}

Related

  1. remove(boolean[] array, boolean value)
  2. remove(int[] a, int[] b)
  3. remove(String[] initial, String... toExclude)
  4. remove(String[] target, String[] needRemoved)
  5. remove(T[] a, int i)
  6. removeAll(T[] array, Collection toRemove)
  7. removeAll(T[] items, T item)
  8. RemoveArgs(String[] args, int startIndex)
  9. removeAt(T[] array, int index)