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.
The solution to this was that I was using a List which is not a collection and Collections.sort()
expects a List
Also is there a better way than singular
import
statements for all my utils?
The solution given was
import java.util.*;
Collections.sort
expects a List
and not Collection
, so change your sortPrint
method
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>();
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With