| Author |
Method to return next object from a List
|
George Vagenas
Greenhorn
Joined: Mar 04, 2008
Posts: 11
|
|
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
Joined: Jul 08, 2003
Posts: 24045
|
|
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.
|
[Jess in Action][AskingGoodQuestions]
|
 |
George Vagenas
Greenhorn
Joined: Mar 04, 2008
Posts: 11
|
|
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
Joined: Oct 13, 2005
Posts: 32611
|
|
|
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
Joined: Mar 04, 2008
Posts: 11
|
|
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
Joined: Dec 22, 2004
Posts: 142
|
|
|
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
Joined: Mar 04, 2008
Posts: 11
|
|
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
Joined: Dec 22, 2004
Posts: 142
|
|
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
Joined: Mar 04, 2008
Posts: 11
|
|
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
Joined: Jul 11, 2001
Posts: 14112
|
|
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.
|
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
|
 |
George Vagenas
Greenhorn
Joined: Mar 04, 2008
Posts: 11
|
|
|
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
Joined: Oct 13, 2005
Posts: 32611
|
|
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.
|
 |
 |
|
|
subject: Method to return next object from a List
|
|
|