java 没有合适的排序错误方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10748522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
no suitable method for sort error
提问by Matthew Optional Meehan
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Collection;
public class ClearlyAnArrayList
{
public static void main(String[] args){
Scanner kb=new Scanner(System.in);
ArrayList<Integer>ints=new ArrayList<Integer>();
int num=kb.nextInt();
while(num!=-1){
ints.add(num);
}
sortPrint(ints);
}
public static void sortPrint(Collection<Integer>ints){
Collections.sort(ints);
for(Integer i:ints){
System.out.printf("%d\n",i);
}
}
}
This is the code I'm compiling with blueJ When I compile it I get a lengthy error which starts off "
no suitable method for sort(java.util.Collection<java.lang.Integer>)
" and then goes on to say more stuff I don't understand.
这是我用 blueJ 编译的代码 当我编译它时,我得到一个冗长的错误,从“
no suitable method for sort(java.util.Collection<java.lang.Integer>)
”开始,然后继续说更多我不明白的东西。
The solution to this was that I was using a List which is not a collection and Collections.sort()
expects a List
对此的解决方案是我使用的是一个列表,它不是一个集合,而是Collections.sort()
一个列表
Also is there a better way than singular
import
statements for all my utils?
import
对于我的所有实用程序,还有比单数语句更好的方法吗?
The solution given was
给出的解决方案是
import java.util.*;
采纳答案by mprabhat
Collections.sort
expects a List
and not Collection
, so change your sortPrint
method
From
Collections.sort
期望 aList
和 not Collection
,所以改变你的sortPrint
方法 From
Collection<Integer>ints
To
到
List<Integer> ints
Offtopic:
无关:
Instead of working directly on concrete classes program to an interface.
而不是直接在具体的类上工作,编程到接口。
Prefer
更喜欢
List<Integer> ints = new ArrayList<Integer>();
Over
超过
ArrayList<Integer> ints = new ArrayList<Integer>();
回答by Shawn
what about just doing this:
只是这样做怎么样:
public static void sortPrint(List<Integer> ints){
Collections.sort(ints);
for(Integer i:ints){
System.out.printf("%d\n",i);
}
Collections.sort()
is only for List
Collections.sort()
只为 List
回答by tibo
For your import
为您的进口
import java.util.*;
The the sort method sort a List, not a collection, so change you declaration to :
sort 方法对列表而不是集合进行排序,因此将您的声明更改为:
public static void sortPrint(List<Integer> ints)
You can also have a collection that sort itself during insertion, your program become :
你也可以有一个在插入过程中自行排序的集合,你的程序变成:
public static void main(String[] args){
Scanner kb=new Scanner(System.in);
TreeSet<Integer>ints=new TreeSet<Integer>();
int num=kb.nextInt();
while(num!=-1){
ints.add(num);
}
//already sorted
}
Warning, in a Set (and so in a TreeSet) you cannot have the same element twice
警告,在 Set 中(在 TreeSet 中也是如此)你不能有两次相同的元素
回答by BachT
Read the documentations (or try to Ctrl + Space for information).
阅读文档(或尝试按 Ctrl + Space 获取信息)。
java.until.Collections
documentation: here
java.until.Collections
文档:这里
This class only supports Collections.sort(list)
and Collections.sort(list,comparator)
, so you can not sort the Collection
. Try List
instead
这个类只支持Collections.sort(list)
和Collections.sort(list,comparator)
,所以你不能对Collection
. 尝试List
代替