epheterson
11-05-2009, 11:49 PM
I'm having a mental block here. My task is to create a class, called node from hence forth, that has attributes such as strings and ints which define it. This node must also reference another node which is to be the next node "in line."
So here's my mental block, where do I create this new node in my class? I want it to always exist, and be accessible. When I place it before the constructor, I end up with an infinite loop kind of situation, rightfully so. As any node is created, it must create another. If I place it within any method, constructor or otherwise, it's stuck in this method.
Where do I define this so that it does not create an infinite loop, but does let me set it to either null or another node within methods.
My work in progress, which should be enough to convey what I'm trying to do:
class node
{
// Declare all variables to be used
String name;
int priority;
// Create the node which will store the next node
node nextNode = new node();
public node()
{
System.out.println("New node created");
}
public node(String name, int priority)
{
System.out.println("New node created and assigned "+name+priority);
this.name = name;
this.priority = priority;
}
public node(String name, int priority, node newNextNode)
{
this.name = name;
this.priority = priority;
this.setNextNode(newNextNode);
}
public String getName(){
return this.name; }
public int getPriority(){
return this.priority; }
public String setName(String name){
this.name = name; }
public int setPriority(int priority){
this.priority = priority; }
public node setNextNode(node newNextNode)
{
nextNode.setName(newNextNode.getName());
nextNode.setPriority(newNextNode.getPriority());
}
public node getNextNode()
{
return nextNode;
}
}
BTW, thanks for any and all help here. CodingForums has helped me out so much in the past and always does.
-Eric
So here's my mental block, where do I create this new node in my class? I want it to always exist, and be accessible. When I place it before the constructor, I end up with an infinite loop kind of situation, rightfully so. As any node is created, it must create another. If I place it within any method, constructor or otherwise, it's stuck in this method.
Where do I define this so that it does not create an infinite loop, but does let me set it to either null or another node within methods.
My work in progress, which should be enough to convey what I'm trying to do:
class node
{
// Declare all variables to be used
String name;
int priority;
// Create the node which will store the next node
node nextNode = new node();
public node()
{
System.out.println("New node created");
}
public node(String name, int priority)
{
System.out.println("New node created and assigned "+name+priority);
this.name = name;
this.priority = priority;
}
public node(String name, int priority, node newNextNode)
{
this.name = name;
this.priority = priority;
this.setNextNode(newNextNode);
}
public String getName(){
return this.name; }
public int getPriority(){
return this.priority; }
public String setName(String name){
this.name = name; }
public int setPriority(int priority){
this.priority = priority; }
public node setNextNode(node newNextNode)
{
nextNode.setName(newNextNode.getName());
nextNode.setPriority(newNextNode.getPriority());
}
public node getNextNode()
{
return nextNode;
}
}
BTW, thanks for any and all help here. CodingForums has helped me out so much in the past and always does.
-Eric