我已经做了一些代码,它从标准输入中获取数字,我想要将这些值按升序排序。我该怎么做?如何把数字以升序排列在Java中的ArrayList中?
import java.util.*;
public class Main {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
boolean Done = false;
ArrayList<Integer> numbers = new ArrayList<Integer>(20);
while(Done == false){
System.out.print("Enter number: ");
int x = sc.nextInt();
numbers.add(x);
System.out.print("Keep going? (Y/N)");
String keepGoing = sc.next();
if(keepGoing.equalsIgnoreCase("Y"))
;
else if(keepGoing.equalsIgnoreCase("N")){
Done = true;
//this is just temporary to print out the numbers.
for(int n : numbers){
System.out.print(n + " ");
}
}
else
System.out.println("Command not recognised...");
}
}
}
很抱歉,如果我张贴已经被回答的问题,但我找不到这似乎与我的代码:(
'Collections.sort(numbers);'。 – Marco13