Java 如何将Compariable用作类中的泛型参数
我在数据结构课程中有一个家庭作业,问题是:Java 如何将Compariable用作类中的泛型参数,java,generics,comparable,Java,Generics,Comparable,我在数据结构课程中有一个家庭作业,问题是: 双链表类的实现 方法: display() length()或size() insertSorted(可比) insertToEnd(可比) insertToHead(可比) 删除(可比) 布尔搜索(可比较) 你必须用JAVA来做这件事 创建应用程序层以测试类及其方法。 将所有源文件压缩到一个文件中,并将其重命名为CS214HW1_first_lastName.zip将您的名字放入文件名中。如果需要,添加一个ReadMe.txt文件以获取编译等额
双链表类的实现 方法:
display()
或length()
size()
insertSorted(可比)
insertToEnd(可比)
insertToHead(可比)
删除(可比)
布尔搜索(可比较)
我正确地实现了所有内容,代码运行良好,但我使用了例如:
insertSorted(int)
而不是insertSorted(Comparable)
,因为我不知道如何做
我在网上搜索并阅读了JAVA文档(Comparable
),但这还不够:(
有人能帮忙吗?这很重要吗
这是我的一些代码,我不能全部写出来,因为我不想让我的朋友得到同样的代码
如果有相同的代码,我将取零
代码:
class DLL {
class Node {
Node next;
Node prev;
int data;
Node() {
next = null;
prev = null;
data = 0;
}
Node(int dt) {
next = null;
prev = null;
data = dt;
}
}
Node head;
void insertToHead(int dt) {
if (head == null) {
head = new Node(dt);
}
else {
head.prev = new Node(dt);
head.prev.next = head;
head = head.prev;
}
}
public static void main(String args[]) {
DLL dll = new DLL();
dll.insertToHead(1);
dll.insertToHead(2);
dll.insertToHead(3);
}
}
有人,请告诉我在
课程开始时要更改什么
我们是否使用了extend
s或implement
sComparable
或什么
对于方法insertToHead(Comparable)
我应该对main
做哪些更改
您可能也希望了解如何工作。基本思想是,您希望设置您的类,以便它不确切地知道对象的特定类型,但可以给出一些提示,说明声明的泛型类型可以预期的类型
在您的例子中,您希望设置列表,以便可以创建可以比较的任何内容的链接列表。Java有一个类
,用于您提到的名为Comparable
的对象。这告诉Java它将能够在提供的对象上调用compareTo
等方法
更具体地说,对于您的结束问题:
使用以下类型的类声明MyClass
。在您的示例中是DLL
将方法参数切换为接受我们声明的泛型类型
您应该使用类整数
而不是原语类型int
,并将列表的创建更改为DLL=new DLL()
提供的代码的完全更新版本:
public class DLL<E extends Comparable<E>> {
class Node {
Node next;
Node prev;
E data;
Node() {
next = null;
prev = null;
data = null;
}
Node(E dt) {
next = null;
prev = null;
data = dt;
}
}
Node head;
void insertToHead(E dt) {
if (head == null) {
head = new Node(dt);
}
else {
head.prev = new Node(dt);
head.prev.next = head;
head = head.prev;
}
}
public static void main(String args[]) {
DLL<Integer> dll = new DLL<Integer>();
dll.insertToHead(1);
dll.insertToHead(2);
dll.insertToHead(3);
}
}
此方法返回一个int
,它将是:
>0
当object1大于object2时
- 当object1等于object2时,
=0
<0
当object1小于object2时
我不想透露太多,因为这是一项家庭作业,但以下是我的提示:
按照上述代码设置类的方式,您可以通过调用以下命令来判断NodeA
和NodeB
之间的关系:
NodeA.data.compareTo(NodeB.data)
这将返回一个整数,根据上面的列表提供您的信息
=
,=
运算符很可能在Integer
类的compareTo
方法中找到
比如:
public int compareTo(Object o) {
int otherNumber = ((Integer) o).intValue();
int thisNumber = this.intValue();
if (otherNumber > thisNumber) {
return 1;
} else if (otherNumber < thisNumber) {
return -1;
} else {
return 0;
}
}
public int compareTo(Object o) {
return this.intValue() - o.intValue(); // possibly normalized to 1, -1, 0
}
参见更多信息。非常感谢,先生,我完全理解这一点,我理解它很快,因为我知道模板是如何在C++中工作的,但现在我知道它是如何在java中工作的。谢谢你,我有一个问题:在方法中,插入排序(可比较)排序意味着它排列哪个更大,哪个更大!我的意思是,只有当该方法获得整数参数时,该方法才有效!,我们如何指定如果我在方法中设置了这样的条件:else if((head!=null)&(head.next==null)&&(head.dataYou know this=运算符仅适用于数字!@TarikSeyceri有关您的问题的信息,请参阅我的编辑。要获得最佳结果,请使用
public int compareTo(Object o) {
return this.intValue() - o.intValue(); // possibly normalized to 1, -1, 0
}