• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

printing...static context?

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm using this code to try to print an Array, and using this code

void display(){
Iterator listIterator = D.iterator();
while(listIterator.hasNext())
{
Disk printDisk =(Disk) listIterator.next();
System.out.println(Disk.toString());
}

}

however, I get the classic method toString cannot be referenced from a static context error. Not sure what I can do about this. Thanks
 
Shaggy Rogers
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
*print an ArrayList actually.
 
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm guessing you meant this, calling toString on the object you got from the iterator:

System.out.println(printDisk.toString());

Instead of this, calling toString on the class Disk:

System.out.println(Disk.toString());
[ October 24, 2006: Message edited by: Carol Enderlin ]
 
Shaggy Rogers
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now I'm getting classCastException errors
 
Shaggy Rogers
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Iterator<DequeInterface<Disk>> listIterator = D.iterator();
while(listIterator.hasNext())
{
DequeInterface printDisk = listIterator.next();
System.out.println(printDisk);
}

NOw I'm using this code and my output looks like:

++++++ The number stored is2 on 0 at left
LinkedDeque@1f6a7b9

that LinkedDeque@1f6a7b9 should be a number.
>_<

I don't know what the I'm doing wrong.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shaggy Rogers:
Iterator<DequeInterface<Disk>> listIterator = D.iterator();
while(listIterator.hasNext())
{
DequeInterface printDisk = listIterator.next();
System.out.println(printDisk);
}

NOw I'm using this code and my output looks like:

++++++ The number stored is2 on 0 at left
LinkedDeque@1f6a7b9

that LinkedDeque@1f6a7b9 should be a number.
>_<

I don't know what the I'm doing wrong.



It looks like your printing your reference to a LinkedDeque object instead of printing out what is stored in that object.
 
Shaggy Rogers
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, I'm just not sure how to print what is stored there
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is a LinkedDeque?

I mean what type of object is it?
 
Shaggy Rogers
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
LinkedDeque is an interface,
and I want to print the contents of the arrayList
ArrayList<DequeInterface<Disk>> D;
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What are the methods in the interface?
 
Shaggy Rogers
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
interface DequeInterface<E> {
// add to left end
void addLeft(E obj);
// add to right end
void addRight(E obj);
// return number of elements in deque
int getSize();
// return reference to left end; deque unchanged; throw exception if empty
E peekLeft();
// return reference to right end; deque unchanged; throw exception if empty
E peekRight();
// if deque size <=1 make empty, else remove one element from both ends
void trimEnds();
}
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like the peek methods are the ones you want.
 
Shaggy Rogers
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alright, I'll give those a try, thanks =D
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You poor fellow -- you seem to have been wrestling with this for days.

You have an ArrayList which contains DequeueInterface objects. If DequeueInterface is (as it appears to be) an interface specifying a double-ended queue, then those queues contains Disk objects. In other words, you have a List of Queues of Disks.

So if you iterate over the ArrayList, each entry is a queue. If you print the queue, then it's the toString() in the DequeueInterface implementation that will be called -- that seems to be a class named "LinkedDequeue" in your case. Based on what you're showing here, that class doesn't override toString() at all, so you just get the version from Object that returns "LinkedDequeue@98739873".

But it looks like you want to print the Disk objects in the queues. So you need to print each Disk in each Queue; in other words, you need TWO loops.

Pardon me, but I have to make something up here: let's hope that DequeueInterface implements Iterable and therefore has a method "iterator()" just like the ArrayList one, which iterates over all the items in the queue. If it doesn't then this code needs to be adjusted, of course.

Anyway, you want to do something like



This would print all the disks in all the queues. You might want to somehow add markers to divide the displays for the different queues; that's up to you.

I hope this helps.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic