Java 如何实现扩展可比较的列表列表?
我试着制作一个通用的链表 此链接列表的节点使用Java 如何实现扩展可比较的列表列表?,java,generics,linked-list,generic-list,comparable,Java,Generics,Linked List,Generic List,Comparable,我试着制作一个通用的链表 此链接列表的节点使用。但是当我使用 LList<LList<Integer>> linkedlist = new LList<LList<Integer>>(); LList linkedlist=new-LList(); 要创建新实例,出现错误: Multiple markers at this line - Bound mismatch: The type LList<Integer> is not a
。但是当我使用
LList<LList<Integer>> linkedlist = new LList<LList<Integer>>();
LList linkedlist=new-LList();
要创建新实例,出现错误:
Multiple markers at this line
- Bound mismatch: The type LList<Integer> is not a valid substitute
for the bounded parameter <T extends Comparable<T>> of the type LList<T>
- Bound mismatch: The type LList<Integer> is not a valid substitute
for the bounded parameter <T extends Comparable<T>> of the type
此行有多个标记
-绑定不匹配:类型LList不是有效的替代项
对于LList类型的有界参数
-绑定不匹配:类型LList不是有效的替代项
对于类型的有界参数
我该如何解决这个问题
节点类:
public class Node <T extends Comparable <T>> {
// Members:
public T data;
public Node <T> next;
// Methods:
public Node () {
data =null;
next = null;
}
public Node (T data) {
this.data = data;
next = null;
}
}
公共类节点{
//成员:
公共数据;
公共节点下一步;
//方法:
公共节点(){
数据=空;
next=null;
}
公共节点(T数据){
这个数据=数据;
next=null;
}
}
利斯特等级:
public class LList <T extends Comparable <T>> {
// Members:
public Node <T> head;
// Methods:
public LList () {
head = null;
}
// Add node.
public void addNode (T data) {
if (head == null) {
head = new Node <T> (data);
return;
}
Node <T> newNode = new Node <T> (data);
Node <T> tempNode = head;
while (tempNode.next != null) tempNode = tempNode.next;
tempNode.next = newNode;
}
// Show linked list.
public void showLLForInteger () {
if (head == null) return;
Node <T> tempNode = head;
while (tempNode != null) {
System.out.print(String.format("%-6d", tempNode.data));
tempNode = tempNode.next;
}
System.out.println();
}
}
公共类LList{
//成员:
公共节点头;
//方法:
公共图书馆(){
head=null;
}
//添加节点。
公共无效添加节点(T数据){
if(head==null){
head=新节点(数据);
返回;
}
Node newNode=新节点(数据);
节点tempNode=头;
而(tempNode.next!=null)tempNode=tempNode.next;
tempNode.next=newNode;
}
//显示链接列表。
public void showlforinteger(){
if(head==null)返回;
节点tempNode=头;
while(tempNode!=null){
System.out.print(String.format(“%-6d”,tempNode.data));
tempNode=tempNode.next;
}
System.out.println();
}
}
在此语句中,您将LList
类作为类型参数<代码>LList不扩展可比性
。
Integer扩展了comparable,但您没有使用Integer类作为类型参数,而是使用了不扩展comparable的LList
所以你得到了一个错误
更改您的LList
类,如下所示:
public class LList <T extends Comparable <T>> implements Comparable<LList<T>>{
@Override public int compareTo(LList<T> list) {
//to do
}
...// your code
}
公共类LList实现了可比较的{
@覆盖公共整数比较(LList列表){
//做
}
…//您的代码
}
T
?你似乎没有在任何地方使用它T extensed Comparable
,这意味着列表的参数必须与自身可比LList
不起作用,因为LList
与自身不可比(它不扩展comparable
)。您确定不只是想要LList
非常感谢,你能告诉我如何处理这个问题吗?我试图让类LList扩展具有可比性,但不能。不应使用原始类型。您在第一个示例中使用了原始类型
Comparable
,在最后一个示例中使用了原始类型LList
。@newacct在我的回答中没有使用原始类型。“LList
”公共类LList实现了Comparable
”
LList <LList<Integer>> subArrsList = new LList <LList<Integer>>();
public class LList <T extends Comparable <T>> implements Comparable<LList<T>>{
@Override public int compareTo(LList<T> list) {
//to do
}
...// your code
}