Reputation: 69
Despite spending a significant amount of time googling for an answer to my predicament and re-reading the chapter on Generics in my Java textbook I cannot seem to fix the problems with the following code:
public class RedBlackTree<I extends Comparable>
{
private int count = 0;
private RedBlackNode root;
private RedBlackNode current;
private RedBlackNode[] stack;
/**
* Inner class used for representing the nodes of the red-black balanced binary search tree object.
*/
private class RedBlackNode implements Comparable
{
private I id;
private boolean is_btree;
private RedBlackNode[] links;
/**
* Constructor for objects of the RedBlackNode class.
*
* @param id The ID of the node.
*/
private RedBlackNode(I id)
{
if (id == null)
{
throw new NullPointerException("ID cannot be null.");
}
this.id = id;
this.is_btree = true;
}
/**
* Function for comparing the RedBlackNode object to another object.
*
* @param obj The object to be compared.
* @return If invocant > passed, returns 1; if invocant < passed, returns -1; if invocant = passed, returns 0.
*/
private int compareTo(Object obj)
{
if (obj instanceof RedBlackTree.RedBlackNode)
{
RedBlackNode node = (RedBlackNode)obj;
int result = id.compareTo(node.id);
return result > 0 ? 1 : result < 0 ? -1 : 0;
}
else
{
throw new ClassCastException("Expected a RedBlackNode object.");
}
}
}
}
In particular, I'm receiving a popup with the following message:
Warnings from last compilation
C:\Users\...\RedBlackTree.java uses unchecked or unsafe operations.
Recompile with -Xlint:unchecked for details.
Nearly every combination of I here or Comparable there still leads to such a popup. I'm using the BlueJ environment for programming and this makes it impossible to incorporate the relevant compiler argument in order to see any details.
As far as I can discern from my research thus far, it's something to do with the fact that the inner class utilizes the I generic type and so the "RedBlackNode implements Comparable" and the compareTo method in the RedBlackNode inner class need to contend with that fact somehow.
I know that this question has been asked here on stackoverflow and elsewhere and answered many times, but I can't seem to apply what I've learned from those instances to my case. I'm pretty new to Generics, so any help I can get here would be very much appreciated!
Upvotes: 1
Views: 1788
Reputation: 109547
Make the following changes
public class RedBlackTree<I extends Comparable<I>>
private class RedBlackNode implements Comparable<RedBlackNode>
@Override
public int compareTo(RedBlackNode node)
int result = id.compareTo(node.id);
return result > 0 ? 1 : result < 0 ? -1 : 0;
In the compareTo remove type checking. Mind the public, therefore always use @Override
.
Upvotes: 2
Reputation: 346260
The warning is because you're using the raw type Comparable
instead of giving it a type parameter. All you have to do is change the class definitions to
public class RedBlackTree<I extends Comparable<I>>
and
private class RedBlackNode implements Comparable<RedBlackNode>
and adjust the compareTo()
method accordingly (which will actually simplify it considerably since you don't need the type checking and casting anymore).
Upvotes: 2
Reputation: 13821
Comparable takes a Type parameter. The compiler complains because you have not supplied it.
Upvotes: 2