• Post Reply Bookmark Topic Watch Topic
  • New Topic

looping in a loop?  RSS feed

 
Mike McMahon
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi -
I have loaded up an array list with objects, and I am trying to pull out some information from them within a loop using the loop below. My result is that I get a tone of information spewed into my output, but it loooks like it is the same information repeated x number of times, where x is the number of items in my arraylist.

I was hoping that it would go to arraylist item1, give me the getName and the getDate, then do the same for all the sequential records.

This has to be some obvious oversight....

 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is exactly what your loop is doing. YOu ahve told it to get the details, print them out, and then lose them from active memory.
If you want to do something with these MMMPaths, you will have to keep them somewhere, or do something in the loop. What, I don't know.

BTW: You are using a bad name for the ArrayList "arrayl". Your repeated = null calls are redundant; your local variable will vanish when the method completes anyway. And a for-each loop might have been better than an old-fashioned for loop.
 
Mike McMahon
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
... Your repeated = null calls are redundant; your local variable will vanish when the method completes anyway...

Thanks Campbell,

The reasoning for the null calls was that I thought that as each object was pulled from the array, I would set it to path_get while I wrote it out the information, then I would clear out the path_get object so that as the loop populated it with new information (from the next value in the arraylist, it would be writing to an empty object.

---Mike's Edit---
Hmmm - it may be that I have just loaded the arraylist incorrectly, and loaded it up with identical objects... I will look into that before I ask too much of you guys...

Thanks,
Mike
[ January 24, 2008: Message edited by: Mike McMahon ]
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mike McMahon:

The reasoning for the null calls was that I thought that as each object was pulled from the array, I would set it to path_get while I wrote it out the information, then I would clear out the path_get object so that as the loop populated it with new information (from the next value in the arraylist, it would be writing to an empty object.


path_get is *not* an object, it is just a reference to an object. Therefore, it also doesn't get populated with information from an object, it just can be made to reference a different object.

Last but not least, "null" doesn't stand for an empty object, it stands for *no* object.

Because of all these reasons, Campbell is right, the assignment of null to the reference in your loop is superfluous.
 
Mike McMahon
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys - I'll just add here for other noob's in case they stumble against a similar problem. My methods were not public (they were static), hence they could not be pulled out and my loop just kept returning the last record again and again and again...

Mike
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!