Why using a class as a variable member inside that said class - java

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.

Related

required type T, Provided Object

I try to implement Generic linked list but I get an error:
required type T, Provided Object in the line T val = head.val;
DDLinkedList class:
public class DDLinkedList <T>{
private ListElement head;
private ListElement tail;
protected <T> void addToHead(T val){
if (this.isEmpty()){
head = new ListElement(val,head);
tail = head;
}else {
head = new ListElement(val, head);
head.next.prev = head;
}
}
protected <T> T removeFromHead(){
if(this.isEmpty()){
return null;
}
T val = head.val;
head = head.next;
return val;
}
}
the List Element class:
public static class ListElement<T> {
private ListElement next;
private ListElement prev;
private T val;
public ListElement(){
this(null, null, null);
}
public ListElement(T val){
this(val, null, null);
}
public ListElement(T val, ListElement next, ListElement prev){
this.val = val;
this.next = next;
this.prev = prev;
}
}
What can be the problem?
Let's have a look at your code (and the changes I suggest regarding Generics):
public class DDLinkedList<T> {
private ListElement<T> head;
private ListElement<T> tail;
Here, you want head and tail to be specific ListElements containing a T value, not just any raw ListElement. That's what ListElement<T> expresses.
protected void addToHead(T val){
Don't use protected <T> void addToHead(T val){, as the <T> introduces a new variable type, incidentally also called T, but unrelated to the intended list element type T.
if (this.isEmpty()){
head = new ListElement<T>(val, head, null);
You'll want to declare that you create a ListElement of element type T. (Your version surely gives warnings on using raw types.) And there's no two-argument constructor for ListElement.
tail = head;
}else {
head = new ListElement<T>(val, head, null);
head.next.prev = head;
}
}
protected T removeFromHead(){
if(this.isEmpty()){
return null;
}
T val = head.val;
As a result of declaring private ListElement<T> head;, the compiler now knows that head.val is of type T
head = head.next;
return val;
}
}
public static class ListElement<T> {
private ListElement<T> next;
private ListElement<T> prev;
In a list with element type T, the next and prev ListElements will surely also contain T.
private T val;
public ListElement(){
this(null, null, null);
}
public ListElement(T val){
this(val, null, null);
}
public ListElement(T val, ListElement<T> next, ListElement<T> prev){
this.val = val;
this.next = next;
this.prev = prev;
}
}
Then, there is the issue that you have private fields in ListElement, but access them from outside. A solution is to keep the fields private, to introduce getters and setters for the fields, and to use the getters and setters instead of the direct field access. Or even better, move the linking logic into the ListElement class, so you don't need the setters at all.
The error is caused because you have redeclared the type T in removeFromHead(); T is already declared in the class declaration so the compiler is trying to equate two different types of the same name.
Redeclare the method to: protected T removeFromHead() and that error should go away. (And you also have the same issue in the other class method.)
As the commenters have noted, you have also missed the type parameter T off all occurrences of ListElement within the declaration of ListElement, which generates separate warnings.
Change the following lines
private ListElement<T> head;
private ListElement<T> tail;

setting a field in a class, does the original object not change the field?

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

Need guidance on creating Node class (java)?

I need to implement a Node class, where the basic methods are: getItem(), getNext(), setItem() and setNext(). I want the nodes to be able to store at least the default integer range in Java as the “item”; the “next” should be a reference or pointer to the next Node in a linked list, or the special Node NIL if this is the last node in the list.I also want to implement a two-argument constructor which initializes instances with the given item (first argument) and next node (second argument) , I've kind of hit a brick wall and need some guidance about implementing this , any ideas ?
I have this so far:
class Node {
public Node(Object o, Node n) {
}
public static final Node NIL = new Node(Node.NIL, Node.NIL);
public Object getItem() {
return null;
}
public Node getNext() {
return null;
}
public void setItem(Object o) {
}
public void setNext(Node n) {
}
}
While implementing the custom LinkedList/Tree, we need Node. Here is demo of creating Node and LinkedList. I have not put in all the logic. Just basic skeleton is here and you can then add more on yourself.
I can give you a quick hint on how to do that:
Class Node{
//these are private class attributes, you need getter and setter to alter them.
private int item;
private Node nextNode;
//this is a constructor with a parameter
public Node(int item)
{
this.item = item;
this.nextNode = null;
}
// a setter for your item
public void setItem(int newItem)
{
this.item = newItem;
}
// this is a getter for your item
public int getItem()
{
return this.item;
}
}
You can create a Node object by calling:
Node newNode = Node(2);
This is not a complete solution for your problem, the two parameter constructor and the last node link are missing, but this should lead you in the correct direction.
Below is a simple example of the Node implementation, (i renamed Item to Value for readability purpose). It has to be implemented somehow like this, because methods signatures seems to be imposed to you. But keep in mind that this is definely not the best way to implement a LinkedList.
public class Node {
public static final Node NIL = null;
private Integer value;
private Integer next;
public Node(Integer value, Node next) {
this.value = value;
this.next = next;
}
public Integer getValue() {
return this.value;
}
public Node getNext() {
return this.next;
}
public void setValue(Integer value) {
this.value = value;
}
public void setNext(Node next) {
this.next = next;
}
public boolean isLastNode() {
return this.next == Node.NIL || Node;
}
}
public class App {
public static void main(String[] args) {
Node lastNode = new Node(92, Node.NIL);
Node secondNode = new Node(64, lastNode);
Node firstNode = new Node(42, secondNode);
Node iterator = firstNode;
do () {
System.out.println("node value : " + iterator.getValue());
iterator = iterator.getNext();
} while (iterator == null || !iterator.isLastNode());
}
}
The node class that will be implemented changes according to the linked list you want to implement. If the linked list you are going to implement is circular, then you could just do the following:
public class Node {
int data;
Node next = null;
public Node(int data){
this.data = data;
}
}
Then how are you going to implement the next node?
You are going to do it in the add method of the circularLinkedList class. You can do it as follows:
import java.util.*;
public class CircularLinkedList {
public CircularLinkedList() {}
public Node head = null;
public Node tail = null;
public void add(int data) {
Node newNode = new Node(data);
if(head == null) {
head = newNode;
}
else {
tail.next = newNode;
}
tail = newNode;
tail.next = head;
}
public void displayList() {
System.out.println("Nodes of the circular linked list: ");
Node current = head;
if(head == null) {
System.out.println("Empty list...");
}
else {
do {
System.out.print(" " + current.data);
current = current.next;
}while(current != head);
System.out.println();
}
}
}

Java - Custom iterator not able to track head of custom linked list

The classes are not complete, but here's what I have so far and I expected the test below to pass.
public class LinkedList<T> extends AbstractSequentialList<T> {
private Node<T> head;
#Override
public boolean add(T element) {
if(head == null) {
head = new Node(element);
}
return true;
}
#Override
public ListIterator<T> listIterator(int index) {
return new LinkedListIterator<>();
}
#Override
public int size() {
return 0;
}
private class LinkedListIterator<T> implements ListIterator<T> {
private Node<T> current;
public LinkedListIterator() {
current = (Node<T>) head;
}
#Override
public boolean hasNext() {
return (current != null && current.getNext() != null)? true : false;
}
#Override
public T next() {
return null;
}
}
}
Here is the Node class.
public class Node<T> {
private T value;
private Node next;
public Node(T value) {
this.value = value;
}
public Node(T value, Node next) {
this.value = value;
this.next = next;
}
public T getValue() {
return value;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
My iterator test is like this.
LinkedList<String> list;
ListIterator<String> iterator;
#Before
public void setUp() throws Exception {
list = new LinkedList<>();
iterator = list.listIterator();
}
#Test
public void testHasNext() throws Exception {
assertThat(iterator.hasNext(), is(false));
list.add("Hello World");
assertThat(iterator.hasNext(), is(true));
}
However, I'm failing on the second assertion. My issue is that the "current" pointer in the iterator is always null even though I'm setting it to the head of the enclosing LinkedList class. How can I fix this? Thanks.
It looks the value of current is set inside the constructor of LinkedListIterator.
It hasn't been updated after you have added an element to the list. This seems to your problem here.
What is wrong is your test, IMO.
You shouldn't expect the iterator to point to the first element if the first element has been added after the iterator has been constructed.
Now, why does your iterator work this way? Because Java is pass-by-value. When you construct an iterator, the iterator receives a copy of the reference to the first node of the list. And at this time, this reference is null, because you haven't added any node yet.
If you really want the iterator to "see" the first node of the list even after it has been constructed, then the iterator needs to get the first node of the list in hasNext(), not in the constructor.

Generics collection implementation java compilation error

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;
}
}

Categories