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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我用blueJ编译的代码当我编译它时,我得到一个冗长的错误,从"
no suitable method for sort(java.util.Collection<java.lang.Integer>)
" 开始,然后继续说更多我不理解的东西.
对此的解决方案是我使用的List不是集合并且Collections.sort()
需要List
import
对于我的所有工具,还有比单数语句更好的方法吗?
给出的解决方案是
import java.util.*;
Run Code Online (Sandbox Code Playgroud)
Collections.sort
期望a List
而不是Collection
,所以改变你的sortPrint
方法From
Collection<Integer>ints
Run Code Online (Sandbox Code Playgroud)
至
List<Integer> ints
Run Code Online (Sandbox Code Playgroud)
无关:
比较喜欢
List<Integer> ints = new ArrayList<Integer>();
Run Code Online (Sandbox Code Playgroud)
过度
ArrayList<Integer> ints = new ArrayList<Integer>();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
15365 次 |
最近记录: |