[英]The method X in the type X is not applicable for the arguments (int)
I am getting the following error when trying to call a method of a generic class with int argument. 尝试使用int参数调用泛型类的方法时出现以下错误。
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
Edit2:修复了public class CDLListTest<T> extends CDLList<T>
应该是public class CDLListTest<T> extends CDLList
It looks like you've written a method called insertAfter(T value)
(which you haven't shown us). 看来您已经编写了一个名为insertAfter(T value)
(尚未显示给我们)。 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. 现在,在处理CDLListTest<T>
时要引用它-其中T
可以是任何类或接口。 Therefore, when you call insertAfter
, the value you pass it must be a T
. 因此,当您调用insertAfter
,您传递的值必须为T
But you're passing it an int
instead of a T
. 但是,您将其传递为int
而不是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
. 更改对insertAfter
的调用以传递T
,或者更改insertAfter
方法的签名,以使其参数的类型为int
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.