Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes printing an arrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "printing an arrayList" Watch "printing an arrayList" New topic

printing an arrayList

Shaggy Rogers

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();
Disk printDisk =(Disk);


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

Joined: Mar 10, 2006
Posts: 26
Now I'm trying something like
int arrayListSize = D.size();
for(int i = 0; i < arrayListSize; i++)
but this is doing the same thing, printing memory locations.
Shaggy Rogers

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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074

"...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:
subject: printing an arrayList
Similar Threads
Error in printing arraylist using jstl
Usage of wildcard?
JSTL arraylist of strings
how to add an array to an ArrayList
printing...static context?