ArrayList, Iterator, Help!

Posted on 2003-03-30
Medium Priority
Last Modified: 2012-05-04
Hi all,

I've creates a list:

List _list = new ArrayList();

I've loaded the list successfully:

 System.out.println(_size +" ==== " + _list.get(_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:

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

(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!

Question by:purrbert
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
LVL 35

Accepted Solution

TimYates earned 1000 total points
ID: 8235400
> 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
LVL 35

Expert Comment

ID: 8235404
  Stops s = new Stops(...)
  _list.add( s );
System.out.println(_size +" ==== " + _list.get(_size));
// end while

Author Comment

ID: 8235462
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!

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI


Author Comment

ID: 8235496
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.;)
LVL 35

Expert Comment

ID: 8235500
hee hee :)

no worries...good luck with it
LVL 35

Expert Comment

ID: 8235510
> 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...


Author Comment

ID: 8235564
I'm an old C program (ie. pointers).
I get it!

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses
Course of the Month13 days, 19 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question