Below is the parent class DblyLinkList
package JavaCollections.list;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class DblyLinkList<T> implements Iterable<T>{
class DListNode<T> {
private T item;
private DListNode<T> prev;
private DListNode<T> next;
DListNode(T item, DListNode<T> p, DListNode<T> n) {
this.item = item;
this.prev = p;
this.next = n;
}
}
.....
}
Below is the derived class LockableList,
package JavaCollections.list;
import JavaCollections.list.DblyLinkList.DListNode;
public class LockableList<T> extends DblyLinkList<T> {
class LockableNode<T> extends DblyLinkList<T>.DListNode<T> {
/**
* lock the node during creation of a node.
*/
private boolean lock;
LockableNode(T item, DblyLinkList<T>.DListNode<T> p,
DblyLinkList<T>.DListNode<T> n) {
super(item, p, n); // this does not work
this.lock = false;
}
}
LockableNode<T> newNode(T item, DListNode<T> prev, DListNode<T> next) {
return new LockableNode(item, prev, next);
}
public LockableList() {
this.sentinel = this.newNode(null, this.sentinel, this.sentinel);
}
.....
}
If class LockableNode<T> extends DListNode<T> in the above code, error:The constructor DblyLinkList<T>.DListNode<T>(T, DblyLinkList<T>.DListNode<T>, DblyLinkList<T>.DListNode<T>) is undefined occurs at line super(item, p, n)
This error is resolved by saying: class LockableNode<T> extends DblyLinkList<T>.DListNode<T>
How do I understand this error? Why it got resolved?
You are redeclaring the type variable T in the inner class. That means that within the inner class, the T of the outer class is hidden and cannot be referred to anymore.
Since you have a non-static inner class, you can just remove the type variable T there:
class DListNode { ... }
because it inherits it from the containing class (and probably you mean that the variables are the same, anyway).
Related
This question already has answers here:
What is PECS (Producer Extends Consumer Super)?
(16 answers)
Closed 4 years ago.
I'm trying to implement binary search tree in java. It can take any object as the data of a node in the tree, as long as that object implements the Comparable interface. This is needed because while putting a new node in the tree, we need to decide, whether the new node is of lesser or greater value compared to its parent. My Node class looks something like the following.
package com.java.ds.bst;
public class Node<T extends Comparable<T>> implements Comparable<T> {
private T data;
private Node<T> left = null;
private Node<T> right = null;
public Node() {
this.data = null;
}
public Node(T data) {
this.data = data;
}
public T getValue() {
return this.data;
}
public Node<T> getLeft() {
return this.left;
}
public Node<T> getRight() {
return this.right;
}
public void setLeft(Node<T> left) {
this.left = left;
}
public void setRight(Node<T> right) {
this.right = right;
}
#Override
public int compareTo(T other) {
return this.data.compareTo(other);
}
}
What I don't understand is, in the class name declaration, why do I need T extends Comparable<T> instead of T implements Comparable<T>?
It is the syntax for generics, T extends Comparable<T> means it will accept anything that is Comparable<T>.
You are correct to use implements Comparable<T> when implementing the interface.
I have an interface named IDedObject and I am making a linkedlist class that uses generics that extend the IDedObject interface. In the linkedList class, I have a function that tries to find a given item based on it's data, and then returns that item. I am getting an error that says
Incompatible types. Required: AnyType. Found: IDedObject
This is what my code looks like:
Interface:
public interface IDedObject {
public int getID();
public void printID();
}
LinkedList:
public class singlyLinkedList<AnyType extends IDedObject> {
public Node<AnyType> endMarker;
public Node<AnyType> beginMarker;
AnyType findID(int ID){
Node curNode = beginMarker;
while(curNode.getNext() != endMarker){
if (curNode.getData().getID() == ID){
return curNode.getData();
}
}
return null;
}
}
class Node<AnyType extends IDedObject>{
public Node(AnyType d, Node<AnyType> n){
data = d;
next = n;
}
public Node getNext(){
return next;
}
public AnyType getData() {
return data;
}
public AnyType data;
private Node<AnyType> next;
private int theSize;
}
I appreciate any help with this. It's for a homework assignment, so i'm required to use a generic the extends IDedObject.
Thanks.
I fixed the problem by changing
Node curNode = beginMarker;
to
Node<AnyType> curNode = beginMarker;
in the findID function in the LinkedList class. Not sure how I missed that.
Can someone explain why I am getting this error: The method compareTo(AVLNode) is undefined for the type AVLNode
Here is a shortened version of my Tree class:
public class AVLTree< E extends Comparable<E> >
{
private AVLNode<E> root ;
public AVLTree()
{
this.root = null;
}
public void insert ( AVLNode<E> item )
{
if( item.getLeft().compareTo(item.getItem() ) < 0) //ERROR IS HERE
{
}
}
}
Below is my a short version of my Node class
class AVLNode <E extends Comparable<E> >
{
private AVLNode<E> item;
private AVLNode<E> left;
public AVLNode ( AVLNode<E> item)
{
this.item = item;
this.left = null;
}
public AVLNode( AVLNode<E> item, AVLNode<E> lft )
{
this.item = item;
this.left = lft;
}
public AVLNode<E> getItem()
{
return this.item;
}
public AVLNode<E> getLeft()
{
return this.left;
}
}
Your Comparable base class assumes you're comparing to Es when you want to compare to AVLNode<E>s
So change it to inherit Comparable<AVLNode<E>> and see what happens.
Your AVLNode class should obviously look like this:
public class AVLNode<E extends Comparable<E>> {
private E item;
//...
public int compareTo(final E obj) {
return this.item.compareTo(obj);
}
Difference:
item should be of type E not AVLNode<E> because you want to store an E and not a AVLNode.
To state, that your AVLNodes are comparable, they could implement Comparable<T> themself by just delegating to E#compareTo() method:
public class AVLNode<E extends Comparable<E>> implements Comparable<AVLNode<E>> {
private E item;
//...
#Override
public int compareTo(final AVLNode<E> other) {
return this.item.compareTo(other.item);
}
}
getLeft() returns an AVLNode. You try and call getLeft().compareTo(...). This requires that AVLNode provides a matching method.
From this:
class AVLNode <E extends Comparable<E> >
{
private AVLNode<E> item;
...
It looks like your nodes can only hold other nodes. Presumably your items should actually be of the comparable type E. Then you can retrieve the item and call compareTo() on that.
That's because AVLNode doesn't have a method called compareTo() if you implement one then the issue should be solved.
You should also decide whether your want to compare E vs E or AVLNode vs AVLNode
I was writing a stack, one with a static Node and other non-static.
public class Stack<E> implements Iterable<E>
{
private int N;
private Node<E> first;
private static class Node<E> // This works fine.
{
private E item;
private Node<E> next;
}
}
But when I try to make the Node non-static, it gives me this warning "The type parameter E is hiding the type E"
public class Stack<E> implements Iterable<E>
{
private int N;
private Node<E> first;
private class Node<E> // This shows warning.
{
private E item;
private Node<E> next;
}
}
A bit of understanding which I have tell me that since static memeber is a member of class so it does not give me a warning but when I make it non-static it belongs to the instance. But its not a clear thought.
It's warning you that you are using the generic argument name E in a scope when it already is defined. Changing the generic argument name for Node would be one way to resolve the warning:
public class Stack<E> implements Iterable<E>
{
private int N;
private Node<E> first;
private class Node<T>
{
private T item;
private Node<T> next;
}
}
But since E is already exists, you should just use that; Node is already generic due to being defined within a generic type (Stack<object>.Node and Stack<String>.Node are different types):
public class Stack<E> implements Iterable<E>
{
private int N;
private Node first;
private class Node
{
private E item;
private Node next;
}
}
Note that this works because Node is not static, therefore it has a reference to the outer Stack<E> object, and because of this E must be defined. If Node is static then it has no real relationship to the outer Stack<E> type other than effectively being within its lexical scope.
If I have this class and I want to initialize a new field of type Element,
how I can do that
public class MyLinkedList{
protected Element head, tail;
public final class Element{
Object data;
int priority;
Element next;
Element(Object obj, int priorit, Element element){
data = obj;
priority = priorit;
next = element;
}
}
}
when I try to do this it gave me an error
public class PriorityTest{
public static void main(String[]args){
MyLinkedList.Element e1 = new MyLinkedList.Element("any", 4, null);
}
}
Make the inner calss static
public class MyLinkedList{
protected Element head, tail;
public static final class Element{
Object data;
int priority;
Element next;
Element(Object obj, int priorit, Element element){
data = obj;
priority = priorit;
next = element;
}
}
public static void main(String[]args){
MyLinkedList.Element e1 = new MyLinkedList.Element("any", 4, null);
}
}
Try this
MyLinkedList.Element e1 = new MyLinkedList().new Element("any", 4, null);
your inner class is not static so you need to create an object of outer class first.