arraylist removerange

  • Thread starter nooneinparticular314159
  • Start date
N

nooneinparticular314159

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!
 
M

Mayeul

(e-mail address removed) 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.
 
D

Daniel Pitts

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.
 

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

Similar Threads

Help in hangman game 1
ArrayList grouping? 2
Tasks 1
The cost of the cheapest routes between cities 3
String to ArrayList 13
Copying into ArrayList 2
ArrayList<String>[] ??? 2
Java type-casting -- Q3 50

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,037
Latest member
MozzGuardBugs

Latest Threads

Top