proteced access

I am trying to use ArrayList’s removeRange method but am getting the compile time error

“removeRange(int,int) has protected access in java.util.ArrayList”

How do I get around this? My code is just an instanciation ArrayList a = new ArrayList (), and then a.removeRange(0, a.size())

Subclass / extend it.

Or … dont use that method. If its protected, you shouldn’t be using it.

Consider:


a.removeAll(a.subList(start,end));

PERFECT…! Just ran across the same probem.

Thanks!

This only works if the the objects stored implement equals(), and is pretty inefficient since it iterates over the lists doing equality checks. subList() is required to return a mutable list backed by the parent, so changes there affect the original. This should be much faster.


a.subList(start,end).clear();

Actually, looking at the source, a.subList(start,end).clear() just calls a.removeRange(start,end), hahahaha…

;D

That can remove the wrong thing if you have duplicate elements in the list.

I only found sublist usefulness a little bit ago. Truly self-reflective api’s are the work of the enlightened.