问题描述
我有以下代码,如何使用带有原始类型的 compareto 方法:
public class mySortertedList<T extends Comparable<T>> extends myLinkedList<T> {
class Node {
Node next = null;
T data;
Node (T x) {
data = x;
}
}
public int compareto (T Obj) {
return this.compareto(Obj);
}
基本上我拥有的是一个排序的链表,它根据 compareto 方法将值添加到列表中。如何确保 compareto 适用于原始类型,至少适用于整数?非常感谢任何帮助!
编辑:为了澄清,这里有更多代码: 主程序:
public static void main(String[] args) {
newList.addTo(4);
newList.addTo(1337);
newList.addTo(30);
newList.addTo(15);
}
我还有其他方法,例如 get(int index)、remove(),即删除列表中的最后一个元素。这里是 mySortertList
private Node start = null;
public void addTo(T x) {
Node newNode = new Node(x);
Node pointer = start;
if (start == null) {
start = newNode;
} else if (size()==1){
if (newNode.data.compareto(start.data)<0) {
newNode.next= start;
start = newNode;
} else {
start.next= newNode;
}
} else {
boolean valuePlaced= false;
Node pointer2= start.next;
while (pointer2 !=null) {
if (newNode.data.compareto(pointer.data)<0) {
newNode.next= start.next;
start.next = newNode;
valuePlaced = true;
} else if (newNode.data.compareto(pointer2.data)<0) {
pointer = newNode;
new.next= pointer2;
valuePlaced = true;
}
pointer = pointer.next;
pointer2 = pointer2.next;
}
if (valuePlaced == false) {
pointer.next= newNode;
}
}
}
解决方法
我不知道我是否对你有好感,但这是我可以给你的建议:
- 假设
compareTo()
在类 Node... (**) - 我使用
LinkedList<T>
而不是您的myLinkedList<T>
进行了测试
以下在我的情况下正常工作。
// we are in file Test.java
public class Test {
public static void main(String... args) {
mySortertedList<Integer> newList = new mySortertedList<>();
newList.addTo(4);
newList.addTo(1337);
System.out.println("newList "+ newList.toString()); // newList,4,1337
newList.addTo(30);
System.out.println("newList "+ newList.toString()); // newList,30,1337
newList.addTo(15);
System.out.println("newList "+ newList.toString()); // newList,15,1337
}
}
class mySortertedList<T extends Comparable<T>> extends LinkedList<T> { // rather than your myLinkedList<T>
private Node start = null;
class Node {
Node next = null;
T data;
Node(T x) {
data = x;
}
public int compareTo(T Obj) { // (**)
if ((Object) Obj.getClass().getName() == "java.lang.Integer") {
return Integer.compare((Integer) this.data,(Integer) Obj);
} else {
return this.compareTo(Obj);
}
}
}
public String toString() {
String s = "";
Node pointer2= start;
while (pointer2 !=null) {
s += ","+ pointer2.data.toString();
pointer2 = pointer2.next;
}
return s;
}
public void addTo(T x) {
Node newNode = new Node(x);
Node pointer = start;
if (start == null) {
start = newNode;
} else if (start.next == null) { // (size()==1){
if (newNode.data.compareTo(start.data)<0) {
newNode.next= start;
start = newNode;
} else {
start.next= newNode;
}
} else {
boolean valuePlaced= false;
if (newNode.data.compareTo(start.data)<0) {
newNode.next= start;
start = newNode;
}
else {
while (valuePlaced == false && pointer.next != null) {
if (newNode.data.compareTo(pointer.next.data) < 0) {
newNode.next = pointer.next;
pointer.next = newNode;
valuePlaced = true;
break;
}
pointer = pointer.next;
}
if (valuePlaced == false) {
pointer.next = newNode;
}
}
}
}
}
要测试,只需创建一个 Test.java 并复制粘贴此代码。
,只需对所有情况使用 compareTo
。
Integer
是一个类,不是一个原始类型。 int
是一个原语,但它不能出现在泛型中。您无需执行任何特殊操作,只需使用 compareTo
,它在 Integer
上运行良好。
不可能让 obj
成为原语(在当前版本的 Java 中)。 obj
将始终是一个对象。