Delete Range of Values from Collection

J

Jason Cavett

I've searched, but there doesn't seem to be any way to easily remove a
range of values from a Collection (specifically a Vector). I did see
that the Vector class has the method removeRange(int fromIndex, int
toIndex) but this is a protected method.

If I am looking in the wrong spot, could someone point me in the right
direction (or, if I have to write my own method, it would be good to
know that too).


Thank you
 
P

Patricia Shanahan

Jason said:
I've searched, but there doesn't seem to be any way to easily remove a
range of values from a Collection (specifically a Vector). I did see
that the Vector class has the method removeRange(int fromIndex, int
toIndex) but this is a protected method.

If I am looking in the wrong spot, could someone point me in the right
direction (or, if I have to write my own method, it would be good to
know that too).

See the subList method.

Patricia
 
T

Tom Hawtin

Jason said:
I've searched, but there doesn't seem to be any way to easily remove a
range of values from a Collection (specifically a Vector). I did see
that the Vector class has the method removeRange(int fromIndex, int
toIndex) but this is a protected method.

You can probably search through the source and see where removeRange is
called from.

From the documentation for List.subList:

"This method eliminates the need for explicit range operations (of the
sort that commonly exist for arrays). Any operation that expects a list
can be used as a range operation by passing a subList view instead of a
whole list. For example, the following idiom removes a range of elements
from a list:

"list.subList(from, to).clear();"

Note that it is a half-open range. The higher index is exclusive.

Tom Hawtin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top