在这里,我试图用这个getSmallestValue方法来整理intV和stringV。尝试了不同的想法,但似乎没有工作。任何人有任何明智的想法如何实现这个getSmallestValue方法?可比较的类型在矢量整数或字符串中排序
public class test {
public static Comparable getSmallestValue(Vector<Comparable> a) {
Comparator com = Collections.reverseOrder();
Collections.sort(a, com);
return (Comparable) a;
}
public static void main(String[] args) {
Vector<Comparable> intV = new Vector<Comparable>();
intV.add(new Integer(-1));
intV.add(new Integer(56));
intV.add(new Integer(-100));
int smallestInt = (Integer) getSmallestValue(intV);
System.out.println(smallestInt);
Vector<Comparable> stringV = new Vector<Comparable>();
stringV.add("testing");
stringV.add("Pti");
stringV.add("semesterGoes");
String smallestString = (String) getSmallestValue(stringV);
System.out.println(smallestString);
}
}
如果您在调试器中逐步了解代码,则会出现几个错误。 ;) – 2011-05-11 10:29:52
几个警告即将到来。但在此刻,重点是让代码能够很容易地工作。我知道代码是错误的,但它编译。 – Splitter 2011-05-11 10:34:24