[SOLVED]
I’m a C++ guy learning Java for (hopefully) an upcoming job. As such I’m practicing implementing a Linked List in Java on my own. I finished implementing a standard doubly linked list and it’s working great. However, I then tried to create an Ordered Linked List and realized the inability to overload the ‘<‘ operator was going to be a severe problem.
I’ve looked through other people’s questions on this site but it still isn’t getting through to me, so I thought I’d post my code and get an answered more tailored to what I’m doing.
Onwards..
The Linked List Class:
public class MyList<T> {
/*** ~Public Interface~ ***/
//insert, delete, size, print, etc.
...
/*** Private Data Members ***/
//node begin, end, T data
....
/** Private node class **/
//Represents the nodes in the list
private class node implements Comparable<T>{ //Don't know if this is right
node next;
node prev;
T data;
node(node p, node n, T d){
next = n;
prev = p;
data = d;
}
@Override
public int compareTo(T o) {
return (data <= o ? 1 : 0); //Get an error still here
}
/** Iterator **/
//iterator class
}
The Ordered Linked List Class:
public class OrderedList<T> extends MyList<T> implements Comparable<T>{ //Pretty sure this is wrong
public void insert(T d){
if(empty()){
push_front(d);
} else {
MyList<T>.MyListIter it = begin();
int i = 0;
//This won't work obviously
for(; i < size() || it.current().compareTo(it.next()) == -1; ++i, it.next()){
//find node to place the new node before it
it.prev() //Need to go back one since we went forward in the loopcheck.
}
}
}
Really at a loss here. How does one achieve something similar to operator overloading so that I can finish this ordered list implementation?
I’m also trying to get a grasp on inheritance in Java, so if you see something wrong in that regard, feel free to chime in about that too.
Thanks all.
UPDATE:
Ok, I made the changes I thought I had to make but I’m still running into errors. Here’s the new code:
The node class within the MyList class:
private static class node<T> implements Comparable<T>{
node<T> next;
node<T> prev;
T data;
node(node<T> p, node<T> n, T d){
next = n;
prev = p;
data = d;
}
public int compareTo(T o) {
return ((Comparable<T>) this.data).compareTo(o);
}
}
Ordered List:
public class OrderedList> extends MyList{
Usage of compareTo:
if(((Comparable<T>) it).compareTo(it.next()) == -1) found = true;
The compiler forced me to do those casts and now the error is:
MyList$MyListIter cannot be cast to java.lang.Comparable
List iterator class looks like:
public class MyListIter{
And it’s inside MyList.
EDIT [SOLVED]
When I was using my iterator, instead of doing it.current() to access the actual data, i was doing it.compareTo(..), but of course my iterator isn’t the data and thus doesn’t know anything about compareTo().
As you stated, you cannot overload operators in Java. This means that your sorted list cannot ever use <= to compare objects of any kind. Rather, you need to rely on the data object to have a
compareTo()method or provide a way for the user of your list to give aComparatorobject. If you wish to strictly limit your list to the first case, you can declare it asThis will guarantee that the objects inserted into your list have a
compareTo()method. Now yourNode.compareTo()function simply delgates the comparison to this method:Allowing custom
Comparators can get a little tricky. I suggest you leave that for after you get a default comparison to work.p.s. Do you want to be able to compare lists to each other? If so, then having
OrderedListimplementComparablemakes sense. However, you might want to leave this for a later exercise after you have your list class working with its basic operations.