运用了java8的boxed()流处理。
import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Scanner; import java.util.stream.Collectors; public class Main { public static void main(String[] arg) { Scanner scan = new Scanner(System.in); StringBuilder builder = new StringBuilder(); while (scan.hasNext()) { scan.nextLine(); String[] input = scan.nextLine().split(" "); int[] inputArr = Arrays.stream(input).mapToInt(Integer::parseInt).toArray(); List<Integer> inputList = Arrays.stream(inputArr).boxed().collect(Collectors.toList()); int orderType = scan.nextInt(); if(orderType==0){ Collections.sort(inputList); }else { Collections.sort(inputList,Collections.reverseOrder()); } for(int i=0;i<inputList.size();i++) { if(i==inputList.size()-1){ builder.append(inputList.get(i)); }else { builder.append(inputList.get(i) + " "); } } } System.out.println(builder); } }