我已經做了一些代碼,它從標準輸入中獲取數字,我想要將這些值按升序排序。我該怎麼做?如何把數字以升序排列在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