I am getting the following error when trying to call a method of a generic class with int argument.
The method insertAfter(T) in the type CDLList<T>.Writer is not applicable for the arguments (int)
The Generic class code is
public class CDLList<T> {
public Element Head;
static int count;
public CDLList(T v)
{
Head = new Element(v);
}
public class Element
{
public T data;
public Element next;
public Element prev;
Element(T v)
{
data = v;
prev=null;
next=null;
count++;
}
public T value()
{
return data;
}
}
public Element head()
{
return Head;
}
public Cursor reader(Element from)
{
Cursor CurrCursor=new Cursor(from);
return CurrCursor;
}
public class Cursor
{
public Element current;
Cursor(Element v)
{
current=v;
}
public Element current()
{
T temp;
temp = current.value();
System.out.println(temp);
return current;
}
public void previous()
{
current = current.prev;
}
public void next()
{
current = current.next;
}
public Writer writer()
{
Writer nwriter = new Writer( current);
return nwriter;
}
}
public class Writer
{
public Element current;
Writer(Element temp)
{
current=temp;
}
public boolean delete()
{
Element Td1,Td2;
Td1 = current.prev;
Td2 = current.next;
current=null;
Td1.next = Td2;
Td2.prev = Td1;
return true;
}
public boolean insertBefore(T val)
{
Element t = new Element(val);
Element t2 = current.prev;
t2.next=t;
current.prev=t;
t.next=current;
t.prev=t2;
return true;
}
public boolean insertAfter(T val)
{
Element t = new Element(val);
Element t1 = current.next;
t.next=t1;
t1.prev=t;
current.next=t;
t.prev=current;
return true;
}
}
}
The class implementing the generic class is
public class CDLListTest<T> extends CDLList<T> implements Runnable {
Cursor cursor;
public CDLListTest(T v) {
super(v);
// TODO Auto-generated constructor stub
Element t1= new CDLList.Element(20);
-----------------------
temp.writer().insertAfter(11); -- Getting error here
It works if I extend the generic class to another child generic class and extend the child generic class to a class which contains the main function.
What am I missing here? It should work since the class is generic, After googling a lot unable to find any answers
Edit: I'm sorry I was quite burned out yesterday when I was posting this question, my apologies. I have edited the question to make it clearer.
Edit2: Fixed it public class CDLListTest<T> extends CDLList<T>
should have been public class CDLListTest<T> extends CDLList
It looks like you've written a method called insertAfter(T value)
(which you haven't shown us). Now, you're referring to it when you're dealing with a CDLListTest<T>
- in which T
could be any class or interface at all. Therefore, when you call insertAfter
, the value you pass it must be a T
. But you're passing it an int
instead of a T
.
Either change your call to insertAfter
to pass a T
, or change the signature of the insertAfter
method so that its parameter is of type int
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.