i was looking for a way to “automate” all the casting needed to have List operations available for plain arrays. the issue is that
public static Object[] arrayRemoveAll(Object[] oA, Object[] removeA) {
List<Object> l = new ArrayList<Object>(Arrays.asList(oA));
l.removeAll(Arrays.asList(removeA));
return l.toArray(new Object[l.size()]);
}
always gives me ClassCastException when i try to cast the object back into its original array type (e.g. if i call (String[])arrayRemoveAll(strA, strA2)).
i understand why this happens, but is there any neat way to circumvent this? i would love to be able to use a general function for some of the standard List operations.
thanks a lot in advance.
How about
Personally, I’d avoid the cost overhead of all the conversions to list and manually write my loops, though. It does not get that much more verbose.
Also, if you are already using Commons Lang, it has ArrayUtils#removeElements.