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...static context? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "printing...static context?" Watch "printing...static context?" New topic
Author

printing...static context?

Shaggy Rogers
Greenhorn

Joined: Mar 10, 2006
Posts: 26
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

Joined: Mar 10, 2006
Posts: 26
*print an ArrayList actually.
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
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

Joined: Mar 10, 2006
Posts: 26
Now I'm getting classCastException errors
Shaggy Rogers
Greenhorn

Joined: Mar 10, 2006
Posts: 26
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.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Mar 10, 2006
Posts: 26
Oh, I'm just not sure how to print what is stored there
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
What is a LinkedDeque?

I mean what type of object is it?
Shaggy Rogers
Greenhorn

Joined: Mar 10, 2006
Posts: 26
LinkedDeque is an interface,
and I want to print the contents of the arrayList
ArrayList<DequeInterface<Disk>> D;
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
What are the methods in the interface?
Shaggy Rogers
Greenhorn

Joined: Mar 10, 2006
Posts: 26
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

Joined: Feb 07, 2005
Posts: 2367
It looks like the peek methods are the ones you want.
Shaggy Rogers
Greenhorn

Joined: Mar 10, 2006
Posts: 26
Alright, I'll give those a try, thanks =D
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
 
Consider Paul's rocket mass heater.
 
subject: printing...static context?
 
Similar Threads
super in static context
get a path
Does performance improves with multithreading
AllocateException with Tomcat 4.1
Recursion