Having trouble redirecting nodes
Hey everyone,
I am working on an assignment that concerns itself with linked lists. Below is the code for two user-defined classes. The class Node contains the instance variables, constructors, accessors, mutators and print method. The class LinkedList is where I need to write methods for starting the list, inserting a new node, deleting a node, and searching the nodes. Each node contains professor information(which is why I have left the arguments as empty strings when creating them (prof1 and prof2))).
So far I have created the three empty nodes head, last, and temp,which serve only as pointers. I am trying to create the list and have created the node prof1. My question is how do I redirect head to point to prof1 instead of null?. If I write head = prof1 I get the error "Syntax error on token";", , expected" which is indicated on the semicolon of Node prof1 = new Node("", "", "", null);
Code :
public class Node {
//icams
//instance variables
private String name;
private String researchArea;
private String email;
private Node link;
//constructors
public Node() {//default constructor
name = "";
researchArea = "";
email = "";
link = null;
}
public Node(String nameIn, String researchIn, String emailIn, Node newLink) {//constructor with parameters.
name = nameIn;
researchArea = researchIn;
email = emailIn;
link = newLink;
}
//accessors
public String getName() {
return name;
}
public String getResearchArea() {
return researchArea;
}
public String getEmail() {
return email;
}
public Node getNode() {
return link;
}
//mutators
public void setName(String nameIn) {
name = nameIn;
}
public void setResearchArea(String researchIn) {
researchArea = researchIn;
}
public void setEmail(String emailIn) {
email = emailIn;
}
public void setNode(Node linkIn) {
link = linkIn;
}
Code :
public class LinkedList extends Node {
Node head = new Node();
Node last = new Node();
Node temp = new Node();
Node prof1 = new Node("", "", "", null);
head = prof1;
//Node prof2 = new Node("", "", "", null);
}
Re: Having trouble redirecting nodes
You're trying to code outside of any method, which isn't possible. Java technically lets you initialize in the class itself, but that's BAD practice (except for constants).
Re: Having trouble redirecting nodes
Thank you, helloworld922. That is a pretty obvious mistake that I should have caught.
Re: Having trouble redirecting nodes
I might sound like a bit of a noob now but how is it bad practise initialising fields in the class rather than the constructor?
Sorry for going off topic.
// Json
Re: Having trouble redirecting nodes
Organization. That's the point of a constructor, to properly set up the object so it can follow it's "contract" (what it's suppose to do). Also, you can do much more inside a constructor than a one-line initialization. Also, chances are some fields are going to inherently require inputs, which that type of initialization is impossible.
The practice is not so bad on static/final fields, though. I still prefer to initialize these type of fields outside of any method :)
Re: Having trouble redirecting nodes
I've always thought that as long as you're consistent either way is fine :)
// Json
Re: Having trouble redirecting nodes
I'm talking about multi-person projects. You'll almost always have a constructor in a class (even if it takes no arguments), and the first (and in many cases the last) place I look is there. Granted, it's not the worst thing you can do :) (probably why they still allow this syntax)