There is no suitable method for sorting error

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 long error starting with " no suitable method for sort(java.util.Collection<java.lang.Integer>)", and then continue to say more things that I don’t understand.

The solution to this was that I used a list that is not a collection but Collections.sort()expects a list

Also is there a better way than singular expressions importfor all my utilities?

The decision was

import java.util.*;
+5
source share
4 answers

Collections.sortexpects Listand not Collection, so change your method sortPrint from

Collection<Integer>ints

For

List<Integer> ints

Offtopic:

, .

List<Integer> ints = new ArrayList<Integer>();

ArrayList<Integer> ints = new ArrayList<Integer>();
+6

:

public static void sortPrint(List<Integer> ints){
    Collections.sort(ints);
    for(Integer i:ints){
    System.out.printf("%d\n",i);
    }

Collections.sort() List

+2

import java.util.*;

, , :

public static void sortPrint(List<Integer> ints)

, , :

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
    }

, Set ( TreeSet)

0

( Ctrl + Space ).

java.until.collections :

suppott Collections.sort(list) Collections.sort(list,compator) , Collection. List

0

All Articles