没有合适的排序错误方法

Mat*_*han 6 java

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)

mpr*_*hat 6

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)