• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 336
  • Last Modified:

ArrayList, Iterator, Help!

Hi all,

I've creates a list:

List _list = new ArrayList();

I've loaded the list successfully:

while(...
_list.add(this);
 System.out.println(_size +" ==== " + _list.get(_size));
_size++;
...
// end while

This gives me a lovely list of the items.  Works great!

HOWEVER, I am trying to iterate through the arraylist once I am out of the while loop.
What I get is the LAST entry of the list (printed "_size" number of times).

Here are a few of my attempts:

1)
for (int i=0; i < _size; i++)
    System.out.println(i +" " + _list.get(i));

2)
(ie. "Stops" is my object)

ListIterator it = _list.listIterator();
  while (it.hasNext()) {
  Stops st = (Stops) it.next();
  System.out.println(" == " + st);
}

Using either 1) or 2) I get a list of my last Object (repeated).
My ultimate goal is to sort these buggers... baby steps. :)

Thank you for your help!

jkatrina@earthlink.net
0
purrbert
Asked:
purrbert
  • 4
  • 3
1 Solution
 
TimYatesCommented:
> while(...
>  _list.add(this);


You are only adding "this" to the Arraylist, which puts a reference to "this" in the list...

You then change the data inside "this", and add another reference to it

WHat you need, is to create a new Stop() obect each time you load one in, then add that new instance to the arrayList every time round the while loop
0
 
TimYatesCommented:
while(...
  Stops s = new Stops(...)
  s.setBlah(...)
  _list.add( s );
System.out.println(_size +" ==== " + _list.get(_size));
_size++;
...
// end while
0
 
purrbertAuthor Commented:
I'm new to this so I hope I'm posting a reply to Tim.

OhMyGod, I did NOT think I would get a solution back this quickly!
I will test it now!

Thanks!
Jill
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
purrbertAuthor Commented:
It WORKED!
I thought I understood "this".
I thought that, for each instance I created as I loaded up the private fields, it would consider that a separate "Stops" thus loading into the ArrayList.
?
The private fields get written over each time so I still don't understand why this wouldn't work.

(I've been working on this all afternoon... thank you so much... I think I love you.;)
0
 
TimYatesCommented:
hee hee :)

no worries...good luck with it
0
 
TimYatesCommented:
> The private fields get written over each time so I still don't understand why this wouldn't work.

It's "sort" of like pointers in other circumstances...

It's hard for me to explain it, so I'll use rubbish metaphors...

Imagine you have a house (your Stops object), with stuff inside it (your private members)

You are adding the street address (reference) to this house into the list.

It then doesn't matter how many times you add the location of this house, it will always be the same house, with the stuff that was in it last time you set it up...

What a rubbish explanation...

Hope it helped anyway...

Tim
0
 
purrbertAuthor Commented:
I'm an old C program (ie. pointers).
I get it!
Thanks!!!!
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now