W
willitheowl
Hello group,
Given a "List<T> objects;" and a "List<R extends Comparable>
weights;", which have the same size and where weights.get(i) is the
weight for objects.get(i), I want to sort the objects according to
weights. (In all that follows, I think, List<T> can be replaced with
T[] without changing the issue.)
To preserve the relationship between objects and weights, I think, I
would be practical to sort indirectly, using a "List<int> index;",
such that after sorting "objects" and "elements" are unchanged,
while weights.get(index.get(0)) is the smallest weight and
weights.get(index.get(1)) the second smallest and so on. (Consequently,
objects.get(index.get(0)) is the object that has the smallest
weight...)
I suppose that this can be implemented by calling the standard
"sort" function with a comparator. The comparison function will
thus look like
public class IndirectComparator<...> implements Comparator<...> // 1
{
public int compare(int i, int j)
{
return weight.compareTo(weight[j]); // 2
}
}
Now it seems that this is not quite correct, since on the line // 2, I
get the warning
"Type safety: The method compareTo(Object) belongs to the raw type
Comparable. References to generic type Comparable<T> should be
parameterized"
Furthermore, what are the proper things to write into the <...> at line
// 1?
thanks for your help and opinions,
Bob
Given a "List<T> objects;" and a "List<R extends Comparable>
weights;", which have the same size and where weights.get(i) is the
weight for objects.get(i), I want to sort the objects according to
weights. (In all that follows, I think, List<T> can be replaced with
T[] without changing the issue.)
To preserve the relationship between objects and weights, I think, I
would be practical to sort indirectly, using a "List<int> index;",
such that after sorting "objects" and "elements" are unchanged,
while weights.get(index.get(0)) is the smallest weight and
weights.get(index.get(1)) the second smallest and so on. (Consequently,
objects.get(index.get(0)) is the object that has the smallest
weight...)
I suppose that this can be implemented by calling the standard
"sort" function with a comparator. The comparison function will
thus look like
public class IndirectComparator<...> implements Comparator<...> // 1
{
public int compare(int i, int j)
{
return weight.compareTo(weight[j]); // 2
}
}
Now it seems that this is not quite correct, since on the line // 2, I
get the warning
"Type safety: The method compareTo(Object) belongs to the raw type
Comparable. References to generic type Comparable<T> should be
parameterized"
Furthermore, what are the proper things to write into the <...> at line
// 1?
thanks for your help and opinions,
Bob