Here you can find the source of removeAll(T[] items, T item)
Parameter | Description |
---|---|
items | a parameter |
public static <T> T[] removeAll(T[] items, T item)
//package com.java2s; //License from project: Open Source License import java.util.Arrays; public class Main { /**//from w w w.j av a2 s .co m * Remove any occurrence of a given value from an array. The resulting array * may be shorter in length, but the relative position of all other items * will remain unchanged. This algorithm is robust to <code>null</code>. The * <code>items</code> array may contain <code>null</code> values and the * <code>item</code> may itself be <code>null</code> (in which case, all * <code>null</code> values are removed). * * @param items * @return */ public static <T> T[] removeAll(T[] items, T item) { int count = 0; // First, determine the number of elements which will be removed for (int i = 0; i != items.length; ++i) { T ith = items[i]; if (ith == item || (item != null && item.equals(ith))) { count++; } } // Second, eliminate duplicates (if any) if (count == 0) { // nothing actually needs to be removed return items; } else { T[] nItems = Arrays.copyOf(items, items.length - count); for (int i = 0, j = 0; i != items.length; ++i) { T ith = items[i]; if (ith == item || (item != null && item.equals(ith))) { // skip item } else { nItems[j++] = ith; } } return nItems; } } }