So, I got this comparable class:
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;}
I think I've got it right...
Then I have my own linked list of generic objects: (no need to read all of it)
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;
}
So far everything works fine, but now, when I'm trying to implement a bubble sort , the item.compareTo() doesn't exist. The way I see it the Item should always have implemented the comparable interface and have access to this method. What am I doing wrong?
Change the implements clause of your
GenericList` from:
public class GenericList<Type extends Comparable> implements Iterable<Comparable>{
To:
public class GenericList<Type extends Comparable<Type>> implements Iterable<Type>{
Just using Comparable
is incomplete by itself; you need to say what it should be able to compare against. An in your case, that should be the type variable Type
.
Throughout your class, you should also change Comparable
to Type
, and in the inner class ListIterator
, you should also define a type variable (but to avoid confusion, don't call it Type
but something else like Type2
, because it will be independent from the type variable Type
in GenericList
)
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(){
// ...