| Author |
printing an arrayList
|
Shaggy Rogers
Greenhorn
Joined: Mar 10, 2006
Posts: 26
|
|
I'm trying to print the arrayList D = new ArrayList<DequeInterface<Disk>>(); using the following method: void display(){ Iterator listIterator = D.iterator(); while(listIterator.hasNext()) { Disk printDisk =(Disk) listIterator.next(); System.out.println(printDisk); } } However, I this prints nothing at all, and previously I was just printing memory locations. Thanks for the help.
|
 |
Rusty Shackleford
Ranch Hand
Joined: Jan 03, 2006
Posts: 490
|
|
|
The addresses that were printed were because of the toString() method in Object. What did you put in the overriden toString method in Disk?
|
"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
|
 |
Shaggy Rogers
Greenhorn
Joined: Mar 10, 2006
Posts: 26
|
|
Now I'm trying something like int arrayListSize = D.size(); for(int i = 0; i < arrayListSize; i++) { System.out.println(D.get(i)); } } but this is doing the same thing, printing memory locations.
|
 |
Shaggy Rogers
Greenhorn
Joined: Mar 10, 2006
Posts: 26
|
|
My toString() looks like this: public String toString() { String result = "The number stored is" + number; return result; }
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12907
|
|
"...printing memory locations..." No, what you see after the @ is not a memory location, it's the hashcode of the object you're printing (in hexadecimal form). You have an ArrayList that contains DequeInterfaces that contain Disks. If you loop through the ArrayList, you are looping through the DequeInterfaces. I guess the toString() method you gave is for class Disk. This code from your post above: prints DequeInterfaces, not Disks.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
 |
I agree. Here's the link: jrebel
|
|
subject: printing an arrayList
|
|
|