Java 向量int或字符串中的可比较类型排序
在本文中,我尝试使用这个getSmallestValue方法对intV和stringV进行分类。尝试了不同的想法,但似乎不起作用。有谁对如何实现这个getSmallestValue方法有什么好主意吗Java 向量int或字符串中的可比较类型排序,java,sorting,Java,Sorting,在本文中,我尝试使用这个getSmallestValue方法对intV和stringV进行分类。尝试了不同的想法,但似乎不起作用。有谁对如何实现这个getSmallestValue方法有什么好主意吗 public class test { public static Comparable getSmallestValue(Vector<Comparable> a) { Comparator com = Collections.reverseOrder();
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);
}
}
公共类测试{
公共静态可比getSmallestValue(向量a){
Comparator com=Collections.reverseOrder();
Collections.sort(a,com);
回报(可比)a;
}
公共静态void main(字符串[]args){
向量intV=新向量();
intV.add(新整数(-1));
intV.add(新整数(56));
整数相加(新整数(-100));
int smallestInt=(整数)getSmallestValue(intV);
系统输出打印项次(smallestInt);
Vector stringV=新向量();
第五条添加(“测试”);
第五条添加(“Pti”);
stringV.添加(“semesterGoes”);
String smallestString=(String)getSmallestValue(stringV);
System.out.println(最小字符串);
}
}
使用。如果您想知道它是如何实现的,可以查看
Vector<Integer> v=new Vector<Integer>();
v.add(22);v.add(33);
System.out.println(Collections.min(v));
Vector v=new Vector();
v、 增加(22);v、 增加(33);
System.out.println(Collections.min(v));
欢迎来到StackOverflow
你的基本问题是你试图把一个向量变成一个整数,但你做不到
可能更有用的是使用向量的第一个元素
我建议你
- 使用列表而不是向量
- 我不会用手工包装
- 使用泛型定义getSmallestValue以避免混淆
public static <N extends Comparable<N>> N getSmallestValue(List<N> a) {
Collections.sort(a);
return a.get(0);
}
public static <N extends Comparable<N>> N getSmallestValue2(List<N> a) {
return Collections.min(a);
}
List<Integer> ints = Arrays.asList(-1, 56, -100);
int min = getSmallestValue(ints);
// or
int min = Collections.min(ints);
public static N getSmallestValue(列表a){
收集.分类(a);
返回a.get(0);
}
公共静态N getSmallestValue2(列表a){
返回集合。最小值(a);
}
List ints=Arrays.asList(-1,56,-100);
int min=getSmallestValue(ints);
//或
int min=Collections.min(ints);
如果您在调试器中单步执行代码,就会出现几个错误。;)有几条警告是真的。但目前的重点是使这段代码能够工作,这样之后就可以轻松地解决其余的问题。我知道代码是错误的,但它确实可以编译。不工作仍然匹配此错误在线程“main”java.lang.ClassCastException中出现异常:java.util.Vector不能在test.getSmallestValue(test.java:17)在test.main(test.java:27)转换为java.lang.Compariable不要使用getSmallestValue函数,而是使用集合。min@Spiltter,这是因为你想把一个向量变成一个整数。我猜您正在调用min()
,但忽略了结果??您是对的,但如果我使用它,那么对于字符串,我必须使用另一种方法,这不是我想要的。否则我会使用Collections.sort(intV);系统输出打印LN(intV);Collections.sort(stringV);系统输出打印LN(stringV);这样我就不需要这个方法,但它不是我要找的方法。这个方法应该支持任何基元类型,并得到最小的值,这就是它的目标method@Splitter:我不太明白您的要求是什么,但您不能在集合中使用基元类型。您不能为其编写泛型函数基本类型。至少在Java中这是不可能的。相反,有一些api,比如guava,支持基本类型的min函数。guava中的Ints.min()函数就是一个例子。是的,列表是更简单的方法,我就是用它来实现的。但我想知道的是如何在向量或数组列表中执行它。vector
和ArrayList
都是List
没有区别。完美的答案,彼得。谢谢这就是它所需要的。收集.分类(a);返回a.get(0);