I am trying to figure out how this works under the hood. I have a feeling i am overlooking this but I have this code.
newNode.setNextNode(root);
root = newNode;
return true;
code for node class
public class Node <T extends Comparable <T>> implements Comparable<T>{
private Node nextNode = null;
private T data;
public Node(T data){
this.data = data;
}
public boolean setNextNode(Node nextNode){
this.nextNode = nextNode;
return true;
}
public Node next(){
return nextNode;
}
public T getData(){
return data;
}
#Override
public String toString() {
return data.toString();
}
#Override
public int compareTo(T o) {
return getData().compareTo((T) nextNode.getData());
}
}
root is now set to a field in my new node.
when then do root = newNode. why when i do root.next does it not just give me back the root?
when I do the setter does it create a complete copy of the object? as I am aware when you make one object equal to another the reference refers to the same object. therefore it can change the original object.
if anyone could explain this would be great
Thank you
I am new to Java and been studying it. So I am having trouble to understand what are these and how it being processed by Java also why are we declaring these variables like that. I mean can you educate me on this?
Public abstract class ListItem {
protected Listitem leftLink = null;
protected Listitem rightLink = null;
protected Object value;
some code here
}
Thanks in advance!
Why declare a Class field which has the ClassName as variable type instead of a int, string... ?
Because the developer needs to. Sometimes, an instance of a class must reference another instance of the same class. A typical example is the LinkedList.
Consider a linked list as a sequence of nodes. Each node knows the next one to be linked. Here would be a naive implementation of a node:
class Node<T> {
private Node<T> next;
private T value;
Node(T value) {
this.value = value;
}
public T getValue() {
return value;
}
void setNext(Node<T> next) {
this.next = next;
}
}
As you can see, the class Node contains a variable member of type Node, to reference the next element of the linked list. Finally, a simplistic implementation of the linked list would be:
class LinkedList<T> {
private Node<T> first;
private Node<T> last;
private int length = 0;
public void add(T value) {
Node<T> node = new Node<T>(value);
if(length != 0) {
last.setNext(node);
last = node;
}
else {
first = node;
last = node;
}
length++;
}
public Node<T> getFirst() {
return first;
}
}
When a new node is added to the collection, the previous last node references it and therefore, becomes the new last node.
For an assignment we were asked to implement a fully generic DoubleLinkedList class, and there was a part where I was unsure of that I need help with. I tried to search online, but I was unable to find an answer.
For the addToEnd and addToFront methods, they each have the return type as BasicDoubleLinkedList and should return a "reference to the current object"
When I try to retun a node, it doesn't allow me to because its not a BasicDoubleLinkedList object. I need to use "this" however, I am unsure how to actually go about doing it.
Here is my code so far
*my inner class node
public class Node <T>
{
private T data;
private Node<T> next;
private Node<T> prev;
public Node()
{
}
}
public class BasicDoubleLinkedList<T> {
private Node<T> head; // points to the head node
private Node<T> tail; // points to the last node
int size;
public BasicDoubleLinkedList<T> addToFront(T data)
{
Node<T> newNode = new Node<T>();
newNode.data = data;
if(head == null)
{
head = newNode;
}
else{
head.prev = newNode;
newNode.next = head;
head = newNode;
}
return null; <------------ I am getting a bit confused somewhere, as I am not sure what to put for the return type that is BasicDoubleLinkedList<T>
}
}
How do I make the node "ahead" for the parameter "head" of the object stop equaling "Null" when I use .getNext() on it? I just get NullPointerException everytime
LinkedNode have parameters LinkedNode and E data.
package sets;
import java.util.Iterator;
import java.util.NoSuchElementException;
class LinkedNodeIterator<E> implements Iterator<E> {
private LinkedNode<E> ahead;
private E data;
public LinkedNodeIterator(LinkedNode<E> head) {
ahead = head;
data = ahead.getData();
}
public boolean hasNext(){
boolean here = false;
LinkedNode<E> currNode = this.ahead;
if(currNode.getNext() != null) here = true;
return (here);
}
//othercode...
}
and
package sets;
public class LinkedNode<E> {
private E data;
private LinkedNode<E> next;
public LinkedNode(E data, LinkedNode<E> next) {
this.data = data;
this.next = next;
}
public E getData(){
return data;
}
public LinkedNode<E> getNext(){
return this.next;
}
}
Given this program, and assuming there are no more methods which modify the ahead variable, the only possible way for the ahead to be null, is in its initialization: In other words, ahead is null only when parameter head is null.
I suggest you two things:
Make sure there are no more methods modifying the ahead variable. One sure way to achieve this is declaring it as final (and also a good practice, too).
Add an assert in the first line of the constructor:
assert head!=null;
... and run your program with "java -ea" to enable assertions.
EDIT :
public class LinkedList<E> {
private class Node {
protected Node next, prev;
protected E data;
protected Node(E dat) {
data = dat;
next = prev = null;
}
}
private Node head, tail;
public LinkedList() {
(head = new Node(null)).next = tail = new Node(null);
tail.prev = head;
tail.next = head.prev = null;
}
public class LinkedListIterator {
private Node current = null;
public synchronized void resetToHead() {
current = head.next;
}
public synchronized void resetToTail() {
current = tail.prev;
}
public synchronized E get() {
if (current!=null) return current.data;
return null;
}
}
}
the problem is that i get the following compilation Error on the emphasized lines :
> Type mismatch: cannot convert from LinkedList<E>.Node<E> to
> LinkedList<E>.Node<E>
what does it mean? and how do i fix this?
btw, the code is only part of the implementation so dont try to logicly figure it out.
--- Edited as the question changes slightly ---
The question is now becoming, how do I have two inner classes coordinate generic types? In short, they don't have to if they are both inner classes of an outer class where the generic type is bound. So even with the public synchronized E get() in the non-generic LinkedListIterator you are returning an E (and it is type safe).
However, if you then reach out to implement java.util.Iterator<E> things fall apart, because that E is based on a different class (interface) so the E has different scoping. How do you fix this? You need to parameterize your Node classes to Node<E> to satisfy that E bindings exist on the implementation of Iterator even when that implementation is being used outside of the scope of it's originating class. This forces Node<E> to be defined statically.
The reason it forces the static definition of Node<E> has to do with garbage collection. An Iterator might still be holding references to Nodes even though the LinkedList is scheduled for garbage collection. Sure, you might be able to keep such a thing from happening with a specific implementation, but the JVM has to allow any implementation (even an errant one).
Perhaps it is easier to explain with code
public LinkedList<E> {
public Iterator<E> iterator() {
return new LinkedIterator(head);
}
// private because we don't want instances created outside of this LinkedList
private class LinkedIterator implements Iterator<E> {
// Right here, needing a parameterized next node will force Node to be static
// static inner classes can exist outside of the scope of their parent
// Since it can exist outside of the parent's scope, it needs it's own generic parameter
private Node<E> next;
LinkedIterator(Node start) {
next = start;
}
public boolean hasNext() {
return next != null;
}
public E next() {
Node<E> retValue = next;
if (retValue != null) {
next = retValue.next;
}
return retValue;
}
}
// must be static because LinkedList might be garbage collected when
// an Iterator still holds the node.
// This E is not the same E as in LinkedList, because it is a E declaration (hiding the above E)
private static Node<E> {
Node<E> next;
Node<E> prev;
E data;
}
}
If you are not careful, you can now wind up back where you started; however, the key is to construct new Node<E> objects when needed in the parent scope. Since that is the same scope where you construct LinkedIterator types, the generic type safety will be ensured.
--- Original post follows ----
By specifying that your node class definition is a Node<E>, you basically create a second, independently scoped generic type E which will hide the outer generic type E in the LinkedList class.
Since none of your classes are static, they will only exist within context of a LinkedList class, which will provide the generics binding. That means you can simplify Node<E> to Node yet still put E class types within the Node class. Same goes for the LinkedListIterator, except that if you want it to implement Iterator you should indicate it implements Iterator<E>.
Due to request, what follows is the code that compiles on my machine, (java 1.6.0_20)
public class LinkedList<E> {
private class Node {
protected Node next, prev;
protected E data;
protected Node(E dat) {
data = dat;
next = prev = null;
}
}
private Node head, tail;
public LinkedList() {
(head = new Node(null)).next = tail = new Node(null);
tail.prev = head;
tail.next = head.prev = null;
}
public class LinkedListIterator {
private Node current = null;
public synchronized void resetToHead() {
current = head.next;
}
public synchronized void resetToTail() {
current = tail.prev;
}
}
}
You overdid it a bit by parametrising the embedded classes. I removed all unnecessary ones.
public class LinkedList<E> {
private class Node {
protected Node next, prev;
protected E data;
protected Node(E dat) {
data = dat;
next = prev = null;
}
}
private Node head, tail;
public LinkedList() {
(head = new Node(null)).next = tail = new Node(null);
tail.prev = head;
tail.next = head.prev = null;
}
public class LinkedListIterator {
private Node current = null;
public synchronized void resetToHead() {
current = head.next;
}
public synchronized void resetToTail() {
current = tail.prev;
}
}
}
Alternatively with a static class Node.
public class LinkedList<E> {
private static class Node<E2> {
protected Node next, prev;
protected E2 data;
protected Node(E2 dat) {
data = dat;
next = prev = null;
}
}
private Node<E> head, tail;
public LinkedList() {
(head = new Node(null)).next = tail = new Node(null);
tail.prev = head;
tail.next = head.prev = null;
}
public class LinkedListIterator {
private Node<E> current = null;
public synchronized void resetToHead() {
current = head.next;
}
public synchronized void resetToTail() {
current = tail.prev;
}
}
}
It doesn't understand that the <E> in LinkedListIterator is the same <E> as the parent class. Just remove from the inner class:
public class LinkedListIterator {
private Node<E> current = null;
public synchronized void resetToHead() {
current = head.next;
}
public synchronized void resetToTail() {
current = tail.prev;
}
}