arraylist removerange

Discussion in 'Java' started by nooneinparticular314159@yahoo.com, Nov 13, 2008.

  1. Guest

    Hello. I have an arraylist of entries. I would like to truncate that
    list to the first n entries. It seems logical to use removeRange,
    however removeRange is protected, so I don't seem to be able to. ie.
    I'm trying to do:

    ArrayList ReturnList;
    ReturnList = GetSortedArrayList(ARatingComparator);

    Integer ArrayListSize = ReturnList.size();

    if (NumberOfEntriesToReturn > ArrayListSize) {
    return ReturnList;
    }

    else{
    ReturnList.removeRange(NumberOfEntriesToReturn,
    ArrayListSize);
    }

    But I can't do this because removeRange is private. I'm not anxious
    to have my entire class extend ArrayList. Is there a good way to get
    removeRange to work?

    Thanks!
    , Nov 13, 2008
    #1
    1. Advertising

  2. Mayeul Guest

    a écrit :
    > Hello. I have an arraylist of entries. I would like to truncate that
    > list to the first n entries. It seems logical to use removeRange,
    > however removeRange is protected, so I don't seem to be able to. ie.
    > I'm trying to do:
    >
    > ArrayList ReturnList;
    > ReturnList = GetSortedArrayList(ARatingComparator);
    >
    > Integer ArrayListSize = ReturnList.size();
    >
    > if (NumberOfEntriesToReturn > ArrayListSize) {
    > return ReturnList;
    > }
    >
    > else{
    > ReturnList.removeRange(NumberOfEntriesToReturn,
    > ArrayListSize);
    > }
    >
    > But I can't do this because removeRange is private. I'm not anxious
    > to have my entire class extend ArrayList. Is there a good way to get
    > removeRange to work?
    >
    > Thanks!


    I think you're supposed to do

    returnList.subList(numberOfEntriesToReturn, returnList.size()).clear();

    The javadoc on List.subList() should confirm this.

    --
    Mayeul
    Mayeul, Nov 13, 2008
    #2
    1. Advertising

  3. Daniel Pitts Guest

    wrote:
    > Hello. I have an arraylist of entries. I would like to truncate that
    > list to the first n entries. It seems logical to use removeRange,
    > however removeRange is protected, so I don't seem to be able to. ie.
    > I'm trying to do:
    >
    > ArrayList ReturnList;
    > ReturnList = GetSortedArrayList(ARatingComparator);
    >
    > Integer ArrayListSize = ReturnList.size();
    >
    > if (NumberOfEntriesToReturn > ArrayListSize) {
    > return ReturnList;
    > }
    >
    > else{
    > ReturnList.removeRange(NumberOfEntriesToReturn,
    > ArrayListSize);
    > }
    >
    > But I can't do this because removeRange is private. I'm not anxious
    > to have my entire class extend ArrayList. Is there a good way to get
    > removeRange to work?
    >
    > Thanks!


    ReturnList.subList(NumberOfEntriesToReturn, ArrayListSize).clear();

    PS. only Classes and CONSTANTS should start with UpperCase, variables,
    methods, and fields should be lowerCase.


    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
    Daniel Pitts, Nov 13, 2008
    #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. jerry051
    Replies:
    2
    Views:
    743
    Oliver Sturm
    Aug 2, 2005
  2. Saravanan Rathinavelu

    Iterate through ArrayList using an another ArrayList

    Saravanan Rathinavelu, Aug 16, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    2,738
    Natty Gur
    Aug 19, 2003
  3. Kaidi
    Replies:
    4
    Views:
    2,367
    Kaidi
    Jan 3, 2004
  4. xz
    Replies:
    16
    Views:
    2,367
  5. Philipp
    Replies:
    6
    Views:
    922
    Arne Vajhøj
    May 28, 2008
Loading...

Share This Page