List help  
Author Message
not





PostPosted: 2004-7-6 8:47:00 Top

java-programmer, List help Hi,
I am new to Java.
I've been working on this project for some time now and I cannot get it
to work.
This is supposed to be a linked list.
I need to use an insert method that alphabetically adds a new title to
the list. This code I have here doesn't have a working compareTo method
but only a test to add node to the list.

This code was provided to me and I must add an insert that will put a new
item in alphabetically order. Don't tell me the answer - I just don't
understand why this doesn't work. I hope that what I've written here is
clear.


Thanks
John


Main looks like this something like this:



main(String [] args){

BookList book = new BookList();
book.add (new Book ("title of book"));
}




// ----------------Test compareTo
public int compareTo(Node a, Node b){
// Since I couldn't get this to work either, I test to see if I can get
any value.

println(a.book.compareTo(b.book)); // doesn't work I don't know how to
compare the titles
return 0;
}

public class BookList
{
private BookNode head;

//----------------------------------------------------------------
// Sets up an initially empty list of books.
//----------------------------------------------------------------
BookList()
{
head = null;
}

//----------------------------------------------------------------
// Creates a new Book object and adds it to the end of
// the linked list.
//----------------------------------------------------------------
public void insert (Book newBook){

BookNode node = new BookNode (newBook);
BookNode current;
BookNode prev;

if (head == null)
head = node;
else
{
current = head;

while (current.next != null){


// Orignally this method looped through the whole list until it found the
end, then add the new
// to the bottom.
// I did a test here to see if I could move the list around.
// This is what I think should happen: save current in prev. get next with
current.
// try to put node in from of current.next. It seems no matter what I try
there's a null pointer
// error. I just can't figure this out.

prev = current;
current = current.next;


node.prev = current.prev;
node.next = current;
node.prev.next = node;
current.prev = node;


}
current.next = node;
}
}


//*****************************************************************
// An inner class that represents a node in the book list. The
// public variables are accessed by the BookList class.
//*****************************************************************
private class BookNode
{
public Book book;
public BookNode next;
public BookNode prev;

//--------------------------------------------------------------
// Sets up the node
//--------------------------------------------------------------
public BookNode (Book theBook)
{
book = theBook;
next = null;
prev = null;
}
}
}
 
Roedy Green





PostPosted: 2004-7-6 9:54:00 Top

java-programmer >> List help On Mon, 05 Jul 2004 19:47:10 -0500, email***@***.com wrote or quoted :

>public int compareTo(Node a, Node b){
> // Since I couldn't get this to work either, I test to see if I can get
>any value.
>
> println(a.book.compareTo(b.book)); // doesn't work I don't know how to
>compare the titles
> return 0;
>}


See http://mindprod.com/jgloss/comparable.html
and http://mindprod.com/jgloss/comparator.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
not





PostPosted: 2004-7-6 10:16:00 Top

java-programmer >> List help Thanks for that. I'll give it a try.


On Tue, 06 Jul 2004 01:53:56 GMT, Roedy Green
<email***@***.com> wrote:
> See http://mindprod.com/jgloss/comparable.html
> and http://mindprod.com/jgloss/comparator.html


Any idea what I'm doing wrong here? I think I am setting prev to current
then current to current.next. then putting the node between the two. Where
is the null pointer coming from?
That's the error I get. I shoule mention that in the main there are three
titles which are the other nodes.

public void insert (Book newBook){

BookNode node = new BookNode (newBook);
BookNode current;
BookNode prev;

if (head == null)
head = node;
else
{
current = head;
while (current.next != null){
prev = current;
current = current.next;

node.prev = current.prev;
node.next = current;
node.prev.next = node;
current.prev = node;

}
current.next = node;
}
}
 
 
Roedy Green





PostPosted: 2004-7-6 11:09:00 Top

java-programmer >> List help On Mon, 05 Jul 2004 21:16:06 -0500, email***@***.com wrote or quoted :

>Any idea what I'm doing wrong here? I think I am setting prev to current
>then current to current.next. then putting the node between the two. Where
>is the null pointer coming from?

you can use a debugger trace the code and watch what happens step by
step. You can also print out references, which gives you a bit of a
clue.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
 
Daniel Sj鯾lom





PostPosted: 2004-7-6 13:30:00 Top

java-programmer >> List help email***@***.com wrote:
> Thanks for that. I'll give it a try.
>
>
> On Tue, 06 Jul 2004 01:53:56 GMT, Roedy Green
> <email***@***.com> wrote:
>
>> See http://mindprod.com/jgloss/comparable.html
>> and http://mindprod.com/jgloss/comparator.html
>
>
>
> Any idea what I'm doing wrong here? I think I am setting prev to
> current then current to current.next. then putting the node between the
> two. Where is the null pointer coming from?
> That's the error I get. I shoule mention that in the main there are
> three titles which are the other nodes.
>
> public void insert (Book newBook){
>
> BookNode node = new BookNode (newBook);
> BookNode current;
> BookNode prev;
>
> if (head == null)
> head = node;
> else
> {
> current = head;
> while (current.next != null){
> prev = current;
> current = current.next;
>
> node.prev = current.prev;
> node.next = current;
> node.prev.next = node;
> current.prev = node;
>
> }
> current.next = node;
> }
> }

Consider what happens if there is only one node in the list (the head)
and you add another node. What does node.prev point to in that case? And
what happens when you add a third node after that?

It is also not clear what exactly you are trying to achieve with the
loop. It reminds me of a quote : "This is not right. It is not even
wrong" :-)
--
Daniel Sj鯾lom
Remove _NOSPAM to reply by mail
 
 
john





PostPosted: 2004-7-7 9:34:00 Top

java-programmer >> List help On Tue, 06 Jul 2004 08:29:38 +0300, Daniel Sj鯾lom
<email***@***.com> wrote:


>> Any idea what I'm doing wrong here? I think I am setting prev to
>> current then current to current.next. then putting the node between
>> the two. Where is the null pointer coming from?
>> That's the error I get. I shoule mention that in the main there are
>> three titles which are the other nodes.

>> public void insert (Book newBook){
>> BookNode node = new BookNode (newBook);
>> BookNode current;
>> BookNode prev;
>> if (head == null)
>> head = node;
>> else
>> {
>> current = head;
>> while (current.next != null){
>> prev = current;
>> current = current.next;
>> node.prev = current.prev;
>> node.next = current;
>> node.prev.next = node;
>> current.prev = node;
>> }
>> current.next = node;
>> }
>> }
>
> Consider what happens if there is only one node in the list (the head)
> and you add another node. What does node.prev point to in that case?

Yes, I see. I had to modify the code that was given to me. Linked lists
are new to me. Christ, I didn't know what one was until the other day. I
think I could argue pretty well that my understand is not much better than
when I didn't know they existed. :P

I really didn't know what question to ask since I was having a hard time
with it.

Is a node simply a pointer to a reference variable? Can I get compare
data with a node? Or is a node ONLY a pointer to next the node? So if I
compared nodes, I'd comparing what the nodes pointed to, right?

> It is also not clear what exactly you are trying to achieve with the
> loop. It reminds me of a quote : "This is not right. It is not even
> wrong" :-)

Hey, wait. I've used that one. Jeez, I'm way off. Remember, I am new
*sniff*
Ignore the loop for a minute. When creating the 'current' reference in
the above code, what exactly happens there?

 
 
Daniel Sj鯾lom





PostPosted: 2004-7-7 15:44:00 Top

java-programmer >> List help john wrote:
> On Tue, 06 Jul 2004 08:29:38 +0300, Daniel Sj鯾lom
> <email***@***.com> wrote:
>
>
>>> Any idea what I'm doing wrong here? I think I am setting prev to
>>> current then current to current.next. then putting the node between
>>> the two. Where is the null pointer coming from?
>>> That's the error I get. I shoule mention that in the main there are
>>> three titles which are the other nodes.
>
>
>>> public void insert (Book newBook){
>>> BookNode node = new BookNode (newBook);
>>> BookNode current;
>>> BookNode prev;
>>> if (head == null)
>>> head = node;
>>> else
>>> {
>>> current = head;
>>> while (current.next != null){
>>> prev = current;
>>> current = current.next;
>>> node.prev = current.prev;
>>> node.next = current;
>>> node.prev.next = node;
>>> current.prev = node;
>>> }
>>> current.next = node;
>>> }
>>> }
>>
>>
>> Consider what happens if there is only one node in the list (the head)
>> and you add another node. What does node.prev point to in that case?
>
>
> Yes, I see. I had to modify the code that was given to me. Linked
> lists are new to me. Christ, I didn't know what one was until the other
> day. I think I could argue pretty well that my understand is not much
> better than when I didn't know they existed. :P

I would suggest you start out with a singly linked list. It is slightly
easier to manage. Also, as a general programming tip, if you do not
understand something, you will not be able to program it. It is better
to work something out on paper first if the problem is too hard.

> I really didn't know what question to ask since I was having a hard
> time with it.
>
> Is a node simply a pointer to a reference variable?

A Node is a datatype, or simply a class. It contains two references
(next and prev) to other nodes. When you are using an object variable
(an instance of a class) you are always using a reference, in effect,
you are always manipulating what a variable refers or points to.

A reference is somewhat similar to a pointer in some other languages,
hence the NullPointerException. Unfortunately, java does not have
pointers! This is a confusing inconsistency in the naming of the
exception. It should be probably be named NullReferenceException instead.

Can I get
> compare data with a node?

Yes. You can compare the data (the book) in this case.

>Or is a node ONLY a pointer to next the node?

Well, a node is simply a node. A variable of type Node is a reference to
a Node object. But a Node object also *contains* references to two other
Node objects, prev and next.

> So if I compared nodes, I'd comparing what the nodes pointed to, right?

Yes. That is, if you write thisNode != thatNode, or thisNode ==
thatNode, you would be comparing the adresses that thisNode and thatNode
are stored at, not the contents of the nodes. To compare the books
contained in the nodes you need to write a Comparator.

>> It is also not clear what exactly you are trying to achieve with the
>> loop. It reminds me of a quote : "This is not right. It is not even
>> wrong" :-)
>
>
> Hey, wait. I've used that one. Jeez, I'm way off. Remember, I am
> new *sniff*

No offense intended. I know how frustrating it can be to debug something
for hours.

> Ignore the loop for a minute. When creating the 'current' reference
> in the above code, what exactly happens there?
>

When you say:

BookNode current;

you are simply creating a variable of type BookNode. It does not refer
or point to anything yet. In fact, if you try to use it directly after
declaring it, the compiler will complain about an unitialized variable.

Later, when you say:

current = head;

You are setting current to refer or point to the same object as the head
variable. If we ignore the loop and look at the statement after it:

current.next = node;

Here, we are setting the current.next field to point to node. If the
loop wasn't there, this would also mean that it sets head.next to point
to node, since head and current point to the same object. But we forgot
to set node.prev to point to current! It is still pointing at null. In a
doubly-linked list, we must always make sure that if someNode.next
points to anotherNode, then anotherNode.prev must point to someNode.

Whew! That was complicated. Or rather not complicated, but longwinded.
The gist of it is, that you cannot use objects directly in java. Instead
you are always accessing an object through a reference. Because spelling
all of this out takes a lot of effort, many java programmers just say
that they are using objects, or passing objects or whatever. This
further confuses newbies. Another confusing thing is that the rules work
differently for primitive types (int, long, float etc.), as opposed to
reference types.

If you are still confused, I suggest you try reading chapter 4 of the
java lanaguage specification (you can get it for free from
java.sun.com). It may be some heavy reading, but it is the
specification. If someone tries to argue that java doesn't work the way
described in there, they are wrong and you should ignore them.
--
Daniel Sj鯾lom
Remove _NOSPAM to reply by mail
 
 
info





PostPosted: 2004-7-8 0:30:00 Top

java-programmer >> List help Ok, thanks for the help. One other thing:


Daniel Sj鯾lom <email***@***.com> wrote in message news:


> It is better
> to work something out on paper first if the problem is too hard.

I did do that, actually. Then it became apparent that I just didn't
understand what was happening. But by that time, I had spent so much
time on total failure that I got exhausted.

> A Node is a datatype, or simply a class. It contains two references
> (next and prev) to other nodes. When you are using an object variable
> (an instance of a class) you are always using a reference, in effect,
> you are always manipulating what a variable refers or points to.
>
OK. Makes sense.


> Can I get
> > compare data with a node?
>
> Yes. You can compare the data (the book) in this case.

OK, I add this in the book class:

public int compareTo(Book a){
return title.compareTo(a.title); // got help on this too
}


if main has:
BookList books = new BookList();
books.add (new Book("b"));
books.add (new Book("e"));

How do you compare a book that has no reference variable?
I tried :
current.compareTo(current.next); I just can't seem to get to the
data.


I'm compeletly baffled by how to access the data. I can't quite
understand why I can't get this. My attempts don't compile.

Thanks again for you help.
 
 
Daniel Sj鯾lom





PostPosted: 2004-7-8 2:01:00 Top

java-programmer >> List help J wrote:

>>Can I get
>>
>>>compare data with a node?
>>
>>Yes. You can compare the data (the book) in this case.
>
>
> OK, I add this in the book class:
>
> public int compareTo(Book a){
> return title.compareTo(a.title); // got help on this too
> }
>
>
> if main has:
> BookList books = new BookList();
> books.add (new Book("b"));
> books.add (new Book("e"));
>
> How do you compare a book that has no reference variable?
> I tried :
> current.compareTo(current.next); I just can't seem to get to the
> data.

Presumably you want to compare the books contained in the nodes. In that
case you would say current.book.compareTo(current.next.book). Is that
what you meant? But you could also add this method to the Node class:

public int compareTo(Node n)
{
return book.compareTo(n.book);
}

Then current.compareTo(current.next) would work just as well.
--
Daniel Sj鯾lom
Remove _NOSPAM to reply by mail
 
 
john





PostPosted: 2004-7-8 11:07:00 Top

java-programmer >> List help
> Presumably you want to compare the books contained in the nodes. In that
> case you would say current.book.compareTo(current.next.book). Is that
> what you meant? But you could also add this method to the Node class:
>
> public int compareTo(Node n)
> {
> return book.compareTo(n.book);
> }
>
Yes, I've done that. (I'll double check though) It won't compile. I don't
know if I'm doing something really stupid. I just can't figure this out.
Spending all this time on this one assignment has caused me not only to
lose focus but my other work has suffered also.
I've added the method to the Node, the booklist and it doesn't work. I've
emailed the instructor but I haven't heard from him. It's like the some
kind of esoteric knowledge I can't know.
I have to put it away for awhile. I'm at the verge of a breakdown, one
foot in it, even.

Thanks for your help. I'm going to try again in a day or two. I REALLY
appreciate getting
pointers. I have to serious consider wether I'm cut out for programming.

> Then current.compareTo(current.next) would work just as well.



--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
 
 
Roedy Green





PostPosted: 2004-7-8 11:26:00 Top

java-programmer >> List help On Wed, 07 Jul 2004 22:07:13 -0500, john <email***@***.com> wrote or
quoted :

>It won't compile.

look up the errormessages at
http://mindprod.com/jgloss/errormessages.html
for some hints on what they really mean.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
 
john





PostPosted: 2004-7-8 12:17:00 Top

java-programmer >> List help Excellent. Thanks.


On Thu, 08 Jul 2004 03:26:18 GMT, Roedy Green
<email***@***.com> wrote:

>
> look up the errormessages at
> http://mindprod.com/jgloss/errormessages.html