• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method to return next object from a List

 
George Vagenas
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I am working on a project where i create a list of myContact objects (List<myContact> from a database and i am looking for a way to create a method that every time i call it returns me the next object out of this list but also keeps record of which object had returned already and return the next one.

To be honest i haven't did a research on the forum since i have no clue on what to search for?
Can you please shed some light over here, some directions?

Thanks a lot in advance...
George
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's what iterators do:



Note that while iterators are most often used in a loop like the above, there's no reason they have to be.
 
George Vagenas
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply. I thought about the iterator and the loop but this way i have to go through all the objects inside the List while I was looking for a way to get the next object every time when i am calling the method.
If i use the iterator.next() without a loop i will manage this and the method will know to return the next object every time?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49827
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lists have a get(int i) method; you can record the index as an int and try the get() method to get the next member.
 
George Vagenas
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So a good practice would be to have a class static field to keep the record of the index of the List?
Any other idea of how could i persist the index?

Thanks
 
Guido Sautter
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Having a static int may or may not be a good idea, depending on what you are actually doing with the List ... If you do not access the List other than by getting the next record, I'd just store a reference an Iterator instead of or in addition to the reference to the List itself. The Iterators of ArrayList will keep the current index internally.
 
George Vagenas
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to have a method that by calling it will return me the next object in the List, so can you please elaborate a little bit more on what you suggest?

Thanks
 
Guido Sautter
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by George Vagenas:
I want to have a method that by calling it will return me the next object in the List, so can you please elaborate a little bit more on what you suggest?


Maybe you could elaborate a little more on the circumstances of your particular problem. There's often (if not always) several different ways solving a particular coding problem. Which way is the appropriate one depends on the circumstances, which are still pretty unclear in this case. From what you told so far, using an Iterator and its next() method will work just fine. So please explain why it does not solve your problem, or just try it out and tell us what problem you're facing.
 
George Vagenas
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand what you mean so here its my problem.

I create a List of object out of a database query and i need to access these objects one by time. So i was thinking to create a method that by calling it, will return the next object (so needs to keep a record of the List index). If i use an iterator inside a for loop i have to deal with all the objects at once (i don't want that) , and if i use an iterator without a for loop then i am always getting back the first object in the List.
So for sure i have to keep a record of the List's index and every time i call the method to return the index+1 object of the List for as long as iterator.hasNext() is true.
Now i am looking for a good practice to keep the index so i was thinking of a class field.
In few words this is what i want to implement and any suggestion is welcome.

Thanks
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by George Vagenas:
if i use an iterator without a for loop then i am always getting back the first object in the List.


Not if you store the iterator somewhere (just the same way you store the List, i.e. in a member variable) instead of creating a new one every time.
 
George Vagenas
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you mean to create a static class field of type Iterator and store the iterator there and just call the iterator.next() every time from the method, right?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49827
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that would work. If you want to share the Iterator between all instances of the class, make it static.

If however you add or delete anything from the List or sort it while the Iterator exists, you will get an Exception.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic