File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "printing an arrayList" Watch "printing an arrayList" New topic
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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13868
    
  10

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