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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:22:22  来源:igfitidea点击:

no suitable method for sort error

java

提问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 importstatements for all my utils?

import对于我的所有实用程序,还有比单数语句更好的方法吗?

The solution given was

给出的解决方案是

import java.util.*;

采纳答案by mprabhat

Collections.sortexpects a Listand not Collection, so change your sortPrintmethod 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.Collectionsdocumentation: here

java.until.Collections文档:这里

This class only supports Collections.sort(list)and Collections.sort(list,comparator), so you can not sort the Collection. Try Listinstead

这个类只支持Collections.sort(list)Collections.sort(list,comparator),所以你不能对Collection. 尝试List代替