[英]generic list java with comparable items
因此,我得到了這個可比的課程:
public class JakeInteger implements Comparable<JakeInteger>{
private int value;
public JakeInteger(int value){
this.value = value;
}
@Override
public int compareTo(JakeInteger other) {
if (other.getValue() < value)
return -1;
if (other.getValue() > value)
return 1;
return 0;
}
public String toString(){
return String.valueOf(value);
}
public int getValue(){return value;}
我想我做對了...
然后,我有自己的通用對象鏈接列表:(無需閱讀所有內容)
public class GenericList<Type extends Comparable> implements Iterable<Comparable>{
private Node first = null;
private int length = 0;
@Override
public Iterator<Comparable> iterator() {return new ListIterator();}
private class ListIterator implements Iterator<Comparable>{
private Node current = first;
@Override
public boolean hasNext() {return current != null;}
public Node getCurrentNode(){
return current;
}
@Override
public Comparable next() {
Comparable item = current.item;
current = current.next;
return item;
}
}
private class Node{
Comparable<Type> item;
Node next;
}
public boolean isEmpty(){
if (first == null)
return true;
return false;
}
public void prepend(Comparable item){
Node second = first;
first = new Node();
first.item = item;
first.next = second;
length++;
}
public void append(Comparable item){
if (length == 0){
prepend (item);
return;
}
Node last = first;
for (int i = 0; i<length-1; i++)
last = last.next;
last.next = new Node();
last.next.item = item;
length++;
}
public Comparable first(){
return first.item;
}
public Comparable pop(){//rest
Comparable oldFirst = first.item;
first = first.next;
return oldFirst;
}
public int getLength(){
return length;
}
// it
public Comparable getByIndex(int Index){
if (Index > this.length - 1)return null;
Iterator<Comparable> it = iterator();
for (int i = 0; i < Index-1; i++){
it.next();
}
return it.next();
}
public Node getNodeByIndex(int Index){
Node node = first;
for (int i = 0; i < Index; i++)
node = node.next;
return node;
}
public void bubbleSort(){
if (length <= 0)
return;
Node tempNode = first;
int R = length -2;
boolean swapped = true;
while (R >= 0 && swapped){
swapped = false;
for (int i = 0; i <= R; i++){
//if(tempNode.item.compareTo((Type) tempNode.next.item) )
}
}
}
private void swapWithNext(Node a){
Comparable itema = a.item;
a.item = a.next.item;
a.next.item = itema;
}
到目前為止,一切正常,但是現在,當我嘗試實現冒泡排序時,item.compareTo()不存在。 我認為Item應該始終實現可比較的接口並可以使用此方法。 我究竟做錯了什么?
從以下位置更改GenericList`的implements clause of your
:
public class GenericList<Type extends Comparable> implements Iterable<Comparable>{
至:
public class GenericList<Type extends Comparable<Type>> implements Iterable<Type>{
僅使用Comparable
本身並不完整。 您需要說出它應該能夠與之進行比較的地方。 在您的情況下,應為類型變量Type
。
縱觀你的類,你也應該改變Comparable
於Type
,並在內部類ListIterator
,你也應該定義一個類型變量(但是為了避免混淆,不要把它Type
但別的東西像Type2
,因為這將是獨立於GenericList
的類型變量Type
)
public Iterator<Type> iterator() {return new ListIterator<Type>();}
private class ListIterator<Type2 extends Comparable<Type2>> implements Iterator<Type2>{
public Type2 next() {
// ...
}
// ...
public void prepend(Type2 item){
// ...
}
public void append(Type2 item){
// ...
}
public Type2 first(){
// ...
public Type2 pop(){
// ...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.