This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes List of Items Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "List of Items" Watch "List of Items" New topic
Author

List of Items

Sege Stephen
Ranch Hand

Joined: Oct 06, 2007
Posts: 51
I have a List that returns list of items, from the list of items returned, I want to say tell it to return just the first 4 or whatever number I set it to.

How do I do that, here is my code



System.out prints all the Items, I have other code in my class that tells it to return items that are published between 5th - 7th. And it now returns 10 items found within that date range. I want only the first 4 of the 10 items to be returned. How do I do that within my code?
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

Take a look at the subList(int fromIndex, int toIndex) method of the List interface. Is that what you want to do?
BV Boose
Ranch Hand

Joined: Jul 26, 2008
Posts: 33
maybe: (add adequate checks for list size)


but since you have to access list more then once, would it make more sense to get it once as an instance variable, then access the different parts of the list as you need it?


Originally posted by Sege Stephen:
I have a List that returns list of items, from the list of items returned, I want to say tell it to return just the first 4 or whatever number I set it to.

How do I do that, here is my code



System.out prints all the Items, I have other code in my class that tells it to return items that are published between 5th - 7th. And it now returns 10 items found within that date range. I want only the first 4 of the 10 items to be returned. How do I do that within my code?
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

p.s. When testing your code, be sure to test the case where the full list is smaller than the number of items you want. You'll need to write some code to handle that situation if you want to handle it gracefully.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: List of Items