• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

printing an arrayList

 
Shaggy Rogers
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The addresses that were printed were because of the toString() method in Object. What did you put in the overriden toString method in Disk?
 
Shaggy Rogers
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My toString() looks like this:

public String toString()
{
String result = "The number stored is" + number;
return result;
}
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"...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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic