• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

List of Items

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at the subList(int fromIndex, int toIndex) method of the List interface. Is that what you want to do?
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
If you settle for what they are giving you, you deserve what you get. Fight for this tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic