I don't understand this...

This is a quick sort code for a double linked list

*********************************************************

import java.io.*;

import java.util.*;

publicclass quickSort

{

public <E>void sort (NodePositionList <E> in)

{

int n=in.size();

if (n<1)return;

NodePositionList<E> L =new NodePositionList<E>();

NodePositionList<E> E =new NodePositionList<E>();

NodePositionList<E> G =new NodePositionList<E>();

while (!in.isEmpty())

{

if (((Comparable)in.last().element()).compareTo(in.last().element()) <0)

L.addLast(in.remove(in.last()));

else

{

if (((Comparable)in.last().element()).compareTo(in.last().element()) ==0)

E.addLast(in.remove(in.last()));

else

G.addLast(in.remove(in.last()));

}

System.out.println("in");

in.mostra();

System.out.println("L");

L.mostra();

System.out.println("E");

E.mostra();

System.out.println("G");

G.mostra();

sort(L);

sort(G);

}

while (!L.isEmpty())

in.addLast(L.remove(in.first()));

while (!E.isEmpty())

in.addLast(L.remove(in.first()));

while (!G.isEmpty())

in.addLast(L.remove(in.first()));

}

}

*********************************************************

This code is not exact because it compare every element whit itself .

But the debug and the excution of this code is exact and don't return another error.

This is the exact code:

*********************************************************

import java.io.*;

import java.util.*;

publicclass quickSort

{

public <E>void sort (NodePositionList <E> in)

{

Object pivot = in.last();

int n=in.size();

if (n<1)return;

NodePositionList<E> L =new NodePositionList<E>();

NodePositionList<E> E =new NodePositionList<E>();

NodePositionList<E> G =new NodePositionList<E>();

while (!in.isEmpty())

{

if (((Comparable)in.last().element()).compareTo(pivot) <0)

L.addLast(in.remove(in.last()));

else

{

if (((Comparable)in.last().element()).compareTo(pivot) ==0)

E.addLast(in.remove(in.last()));

else

G.addLast(in.remove(in.last()));

}

System.out.println("in");

in.mostra();

System.out.println("L");

L.mostra();

System.out.println("E");

E.mostra();

System.out.println("G");

G.mostra();

sort(L);

sort(G);

}

while (!L.isEmpty())

in.addLast(L.remove(in.first()));

while (!E.isEmpty())

in.addLast(L.remove(in.first()));

while (!G.isEmpty())

in.addLast(L.remove(in.first()));

}

}

*********************************************************

why if i change the compareTo(...) the debug is exact but the excution return me DNode cannot be cast to java.lang.String?

Thank you...

You can find the rest of class in

http://forum.java.sun.com/thread.jspa?threadID=5143594&tstart=0

[5178 byte] By [imthehella] at [2007-11-26 20:20:05]
# 1

> ...

> why if i change the compareTo(...) the debug is exact

> but the excution return me DNode cannot be cast to

> java.lang.String?

Somewhere in your code you're trying to compare a DNode to a String object. Needless to say, this cannot be done: you cannot compare a pear with an apple.

prometheuzza at 2007-7-10 0:44:11 > top of Java-index,Java Essentials,Java Programming...
# 2
ok i find the problem.. Object pivot = in.last().element(); I missed .element();but now he said me "the linked list is empty"why?it's impossible..
imthehella at 2007-7-10 0:44:11 > top of Java-index,Java Essentials,Java Programming...
# 3

> ok i find the problem..

>

> Object pivot = in.last().element();

>

> I missed .element();

>

> but now he said me "the linked list is empty"

>

> why?

>

> it's impossible..

I'm getting the impression you're not really trying to solve this, but just post whatever your compiler spits at you.

Try to debug your code by putting some System.out.println()'s here and there in your code so you know what variables get changed at a certain time. That way you'll be able to see what's goig wrong.

Good luck.

prometheuzza at 2007-7-10 0:44:11 > top of Java-index,Java Essentials,Java Programming...
# 4
no no i try to solve it for two day and now i don't know which is the problem...The list is not empty....it's impossible!
imthehella at 2007-7-10 0:44:11 > top of Java-index,Java Essentials,Java Programming...
# 5

> no no i try to solve it for two day and now i don't

> know which is the problem...

As soon as you found you were doing something wrong with the element() thing, you posted the next compiler error here. That's what I meant. First have a goood look at your code.

> The list is not empty....it's impossible!

Like I said: debug your code!

prometheuzza at 2007-7-10 0:44:11 > top of Java-index,Java Essentials,Java Programming...
# 6

ok now i found the problem but only translate

Object pivot = in.last().element();

in this way:

public <E> void sort (NodePositionList <E> in)

{

int n=in.size();

if (n<1) return;

NodePositionList<E> L = new NodePositionList<E>();

NodePositionList<E> E = new NodePositionList<E>();

NodePositionList<E> G = new NodePositionList<E>();

Object pivot = in.last().element();

while (!in.isEmpty())

{

if (((Comparable)in.last().element()).compareTo(pivot) <0)

L.addLast(in.remove(in.last()));

and i don't understand because only translate one row of code in another equivalent position the list is not empty and the execution of the program is exactly!

imthehella at 2007-7-10 0:44:11 > top of Java-index,Java Essentials,Java Programming...
# 7
> ...> and i don't understand > ...Me neither.Good luck with it though.
prometheuzza at 2007-7-10 0:44:11 > top of Java-index,Java Essentials,Java Programming...
# 8
> This is a quick sort code for a double linked list... Is this an excercise? Because if not, it seems to me that you could use a LinkedHashMap, which is an implementation of a double linked list anyway.
YoungWinstona at 2007-7-10 0:44:12 > top of Java-index,Java Essentials,Java Programming...
# 9
> ...> Is this an excercise? Because if not, ...If not, I'll eat my hat!
prometheuzza at 2007-7-10 0:44:12 > top of Java-index,Java Essentials,Java Programming...
# 10

> no no i try to solve it for two day and now i don't

> know which is the problem...

>

> The list is not empty....it's impossible!

Then I guess we've arrived at a paradox and the universe will end now.

Or, just maybe, you might be mistaken in one or more assumptions about what your code oes.

jverda at 2007-7-10 0:44:12 > top of Java-index,Java Essentials,Java Programming...
# 11
yes this is en exercise!ok but i don't find a mistake..It a real paradox!i hope you are able to find the error....therefore is a real paradox!
imthehella at 2007-7-10 0:44:12 > top of Java-index,Java Essentials,Java Programming...
# 12

I'm not going to read all that code. I'm not going to run it.

If the VM is telling you something, it's not lying or mistake. So you're making a mistake somewhere. In theory, you know what your code does, which steps it follows and what happens at each step. But obviously you're wrong about at least one part of that.

So, since you think your code is doing one thing, but it's actually doing another thing, what could you possibly do about that? I have an idea! How about if you actually watch your code and see where it matches what you expect and where it does something different.

(*** You were already given this advice here. Twice, I believe. ***)

Use println statements or a debugger to observe what's going on. Predict what you think should happen and compare that to what actually happens. Once you find where reality diverges from assumption, work backwards to figure out why.

Start small. Use a small list--first 1 element, then 2, then 3, then 4, to get the corner cases but still keep it small enough to be manageable.

Corner cases:

1 element: head and tail are the same node, no intermediate nodes

2 elements: no intermediate nodes

3 elmeents: only one intermediate node

The 4 element case gives you a separate head and tail and > 1 intermediate node, so should be representative of the general case.

Also, make sure your code is broken down into small methods that you can test independently of each other. Test m1() by itself, then test m2() by itself. Once they both work, test m1() calling m2(). If that doesn't work, then something's wrong in how you put them together, since you already tested them seperately.

jverda at 2007-7-10 0:44:12 > top of Java-index,Java Essentials,Java Programming...
# 13

> ok but i don't find a mistake..

Because you're not looking hard enough. We have told you how to find it. It's up to you to execute those steps. We'll help you figure out what you have to do, but it's up to you to do the work.

> It a real paradox!

No, it's you not letting go of your assumptions and not taking the time and effort to examine where they're incorrect.

> i hope you are able to find the error

I'm not even going to try. I have enough of my own and my coworkers' and vendor's code to debug. I'm not going to debug yours.

jverda at 2007-7-10 0:44:12 > top of Java-index,Java Essentials,Java Programming...
# 14

ok! but finally i find the error e now the quickSort does it's work!

but i don't understand becouse this code return the error "the linked list is empty"

...

public <E> void sort (NodePositionList <E> in)

{

int n=in.size();

Object pivot = in.last().element();

if (n<1) return;

NodePositionList<E> L = new NodePositionList<E>();

NodePositionList<E> E = new NodePositionList<E>();

NodePositionList<E> G = new NodePositionList<E>();

while (!in.isEmpty())

{

if (((Comparable)in.last().element()).compareTo(pivot) <0)

L.addLast(in.remove(in.last()));

...

and this work perfectly

...

public <E> void sort (NodePositionList <E> in)

{

int n=in.size();

if (n<1) return;

NodePositionList<E> L = new NodePositionList<E>();

NodePositionList<E> E = new NodePositionList<E>();

NodePositionList<E> G = new NodePositionList<E>();

Object pivot = in.last().element();

while (!in.isEmpty())

{

if (((Comparable)in.last().element()).compareTo(pivot) <0)

L.addLast(in.remove(in.last()));

...

i had only moved [code]Object pivot = in.last().element();[code]!

i don't understand this..

i moved the code in an equivalent position...i hope!

Tell me if not e why!

Excuse me for my english but i'm italian and i don't know english very well!

imthehella at 2007-7-10 0:44:12 > top of Java-index,Java Essentials,Java Programming...
# 15

> ...

> i don't understand this..

>

> i moved the code in an equivalent position...i hope!

>

> Tell me if not e why!

Debug your code!

> Excuse me for my english but i'm italian and i don't

> know english very well!

Perhaps it's better if you find a forum or messageboard where people speak your language: I'm under the impression you don't understand the advice given to you.

prometheuzza at 2007-7-21 17:54:41 > top of Java-index,Java Essentials,Java Programming...
# 16
Metta a punto il vostro codice!
CaptainMorgan08a at 2007-7-21 17:54:41 > top of Java-index,Java Essentials,Java Programming...
# 17

no I just used your advice and i understand what you wrote...

do you try to debug my code?

If you able to find the cause of the error then you can say me....you don't understand, you don't try, ecc,ecc...

I write in this forum because i hope to find expert people becouse in many mode i don't found the error also using you advice!

imthehella at 2007-7-21 17:54:41 > top of Java-index,Java Essentials,Java Programming...
# 18

> no I just used your advice and i understand what you

> wrote...

>

> do you try to debug my code?

No, thank you. I am not too keen on compiling and debugging your messy code.

> If you able to find the cause of the error then you

> can say me....you don't understand, you don't try,

> ecc,ecc...

Yes, that's it: I don't understand your code.

And I don't want to understand it.

> I write in this forum because i hope to find expert

> people becouse in many mode i don't found the error

> also using you advice!

What can I say? Too bad.

Best of luck to you though.

prometheuzza at 2007-7-21 17:54:41 > top of Java-index,Java Essentials,Java Programming...
# 19

> do you try to debug my code?

Why would we do that? Most people who answer questions here have enough of their own code to debug. We'll help *YOU* to debug *YOUR OWN* code, but most people here don't want to debug it for you.

> If you able to find the cause of the error then you

> can say me....you don't understand, you don't try,

> ecc,ecc...

I'm sure almost anybody here can find your error quite easily. But even if we can't, we still have the right to chastise you for not trying.

jverda at 2007-7-21 17:54:41 > top of Java-index,Java Essentials,Java Programming...
# 20
nooo you don't understand!Now the code is exctly and i don't have another problem!I want only say that I did not succeed to explain to me as a error was produced!But if you don't want to try my code ... you make less!but not say me that I don't understand!
imthehella at 2007-7-21 17:54:41 > top of Java-index,Java Essentials,Java Programming...
# 21

> nooo you don't understand!

>

> Now the code is exctly and i don't have another

> problem!

>

> I want only say that I did not succeed to explain to

> me as a error was produced!

>

> But if you don't want to try my code ... you make

> less!

>

> but not say me that I don't understand!

The only thing I understand about this post is that things are working as they're supposed to now. That's good, glad you got things working.

The rest of it however is complete gibberish to me. Perhaps you need to consult an English dictionary before posting, or find a forum where people write in your native language.

prometheuzza at 2007-7-21 17:54:41 > top of Java-index,Java Essentials,Java Programming...
# 22
>Yes, that's it: I don't understand your code.>And I don't want to understand it.if you don't want to understand my code..stay in hush!I don't want your unpleasant councils!
imthehella at 2007-7-21 17:54:41 > top of Java-index,Java Essentials,Java Programming...
# 23
if you understand me there's no problem....in my native language there's any forum
imthehella at 2007-7-21 17:54:41 > top of Java-index,Java Essentials,Java Programming...