我試圖用compareTo方法寫一個非常簡單的代碼,用來比較下面的字元串,但是我有一個問題,它按字母順序排序,
String : {BANANA, CAT, BEAR}.
它應該是BANANA, BEAR, CAT.
謝謝!
下面是方法。public void removeMaximumValues(int N) {
if (N < 0) {
return;
}
for (int i = 0; i < N; i++) {
Node current = head;
Node maxNode = null;
int greatestDiff = 0;
//EXTRACT the MaxNode
while (current != null) {
if (head.value.compareTo(current.value) < greatestDiff) {
greatestDiff = head.value.compareTo(current.value);
maxNode = current;
}
current = current.next;
}
.
.
.
//test
public static void main(String[] args) {
MyLinkedList myList = new MyLinkedList();
myList.add("BANANA");
myList.add("CAT");
myList.add("BEAR");
... //then I want to pull"CAT" out of the LinkedList with compareTo method