Delete Range of Values from Collection

Discussion in 'Java' started by Jason Cavett, Mar 6, 2007.

  1. Jason Cavett

    Jason Cavett Guest

    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
     
    Jason Cavett, Mar 6, 2007
    #1
    1. Advertising

  2. Jason Cavett wrote:
    > 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
     
    Patricia Shanahan, Mar 6, 2007
    #2
    1. Advertising

  3. Jason Cavett

    Tom Hawtin Guest

    Jason Cavett wrote:
    > 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
     
    Tom Hawtin, Mar 6, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    46
    Views:
    996
    Antoon Pardon
    Jul 25, 2006
  2. Øyvind Isaksen
    Replies:
    1
    Views:
    1,023
    Øyvind Isaksen
    May 18, 2007
  3. Lambda
    Replies:
    2
    Views:
    424
    James Kanze
    Jul 16, 2008
  4. Tomoyuki Kosimizu

    Range does not take an Range object.

    Tomoyuki Kosimizu, Nov 25, 2003, in forum: Ruby
    Replies:
    3
    Views:
    170
    Tomoyuki Kosimizu
    Nov 27, 2003
  5. David Bird
    Replies:
    1
    Views:
    241
    Tiago Macedo
    Jun 23, 2008
Loading...

Share This Page