LinkedList的Java compareTo 公共类节点{ 私人电子数据; 私有节点下一步; 公共节点(){ next=null; } 公共节点(E数据输入){ 数据=数据输入; } }
///LinkedList的Java compareTo 公共类节点{ 私人电子数据; 私有节点下一步; 公共节点(){ next=null; } 公共节点(E数据输入){ 数据=数据输入; } },java,linked-list,comparable,Java,Linked List,Comparable,/// 类链接列表{ 公共作废插入(E数据输入){ 如果(ctr!=0){ 节点温度=新节点(数据输入); 节点光标=头部; 节点prev=光标; while(cursor!=null&&cursor.data.compareTo(dataIn)
类链接列表{
公共作废插入(E数据输入){
如果(ctr!=0){
节点温度=新节点(数据输入);
节点光标=头部;
节点prev=光标;
while(cursor!=null&&cursor.data.compareTo(dataIn)<0){
prev=光标;
cursor=cursor.next;
}
上一个=下一个=温度;
temp.next=光标;
}
其他的
添加(数据输入);
++ctr;
}
}
在我的insert函数中,如何让Java知道cursor.data与dataIn的类型相同?(假设它们都是整数)很抱歉,我为这个愚蠢的问题道歉。我是一个noob,我不知道在哪里写'compareTo'函数,因为我使用的是整数,而不是自定义数据类型。所以,当我编译代码时,我得到了这个错误
class LinkedList<E>{
public void insert(E dataIn){
if(ctr != 0){
Node<E> temp = new Node<E>(dataIn);
Node<E> cursor = head;
Node<E> prev = cursor;
while(cursor != null && cursor.data.compareTo(dataIn) < 0){
prev = cursor;
cursor = cursor.next;
}
prev.next = temp;
temp.next = cursor;
}
else
add(dataIn);
++ctr;
}
}
必填项:E#1
找到:没有参数
原因:实际参数列表和正式参数列表长度不同
其中E#1、E#2是类型变量:
E#1扩展类LinkedList中声明的对象
E\35; 2扩展类节点中声明的对象
java:34:错误:找不到符号
while(cursor!=null&&cursor.data.compareTo(dataIn)<0){
^
符号:方法比较(E#1)
位置:E#2型变量数据
其中E#1、E#2是类型变量:
E#1扩展类LinkedList中声明的对象
E#2扩展类节点中声明的对象
提前感谢您的关注。!用以下方式定义list类
required: E#1
found: no arguments
reason: actual and formal argument lists differ in length
where E#1,E#2 are type-variables:
E#1 extends Object declared in class LinkedList
E#2 extends Object declared in class Node
LinkedList.java:34: error: cannot find symbol
while(cursor != null && cursor.data.compareTo(dataIn) < 0){
^
symbol: method compareTo(E#1)
location: variable data of type E#2
where E#1,E#2 are type-variables:
E#1 extends Object declared in class LinkedList
E#2 extends Object declared in class Node
公共类链接列表{
私有静态类节点{
并确保
节点
类嵌套为静态(在这种情况下可以是私有
,包私有
)。之后,可以将实现<代码>可比<代码>的任何类型添加到链接列表中。可能<代码>链接列表,因为这似乎是应该是公共的容器类,而节点实际上可以保持非公共。是的…更新我的answer@pizza如果这回答了你的问题,不要费心点击勾选标记否这是一个很好的答案,按回答进行勾选将避免其他论坛访问者不必要的访问。@Maxim非常感谢它现在起作用了,我很感激!但是为什么不勾选标记呢?我不应该这样做吗?
required: E#1
found: no arguments
reason: actual and formal argument lists differ in length
where E#1,E#2 are type-variables:
E#1 extends Object declared in class LinkedList
E#2 extends Object declared in class Node
LinkedList.java:34: error: cannot find symbol
while(cursor != null && cursor.data.compareTo(dataIn) < 0){
^
symbol: method compareTo(E#1)
location: variable data of type E#2
where E#1,E#2 are type-variables:
E#1 extends Object declared in class LinkedList
E#2 extends Object declared in class Node
public class LinkedList<E extends Comparable<E>> {
private static class Node<E> {