Re: unchecked call to compareTo(T)
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Mark schreef:
Writing it as follows seems to fix the problem
class BST<T extends Comparable<? super T>>
{
class Node
{
T obj = null;
Node left = null, right = null;
Node(T obj) {
this.obj = obj;
}
}
...
But now I'm having trouble declaring a new BST. I get the errors...
BST.java:196: unexpected type
found : int
required: reference
BST<int> tree = new BST<int>();
^
BST.java:196: unexpected type
found : int
required: reference
BST<int> tree = new BST<int>();
^
2 errors
Terminated with exit code 1.
I'm not sure what it means...it expects a reference? Why won't it
accept "int"?
int is a primitive type, it does not implement anything, so most
certainly not Comparable. The moment you generified BST, you demanded
its objects to implement Comparable. Anyway, there is not way to store
an int in a collection, so you want to use Integer anyway.
It seems to accept Integer though...why is that?
Because Integer implements Comparable<Integer>.
To answer your other post:
Could someone explain what the <? super T> part means?
Comparable itself is generic. This means you have to give it a
parameter, hence the errors didn???t disappear at your first try, since
you just declared BST<T extends Comparable>.
Now why the super T? Often, a class SuperClass will declare it
implements Comparable. Most often, it will be comparable with itself,
so it will implement Comparable<SuperClass>. (E.g. look at the
definition of Integer.) However, if subclasses of this class are made,
they, too, implement Comparable<SuperClass>. You cannot redefine them
to implement Comparable<SubClass>, the compiler won???t let you. But you
do want to accept these classes in BST. That???s where the super comes
in: it tells the compiler that a class should be comparable to itself or
to some class higher up in the hierarchy.
HTH,H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
iD8DBQFFSy7He+7xMGD3itQRArz5AJ9S1vn1CS7pJTlqg2HhxsiX5KD+DQCfVUto
Xj1WAw3Lc6a3AXX8ycTqNwM=
=CDaC
-----END PGP SIGNATURE-----