题解 | #输入整型数组和排序标识,对其元素按照升序或降序进行排序#
输入整型数组和排序标识,对其元素按照升序或降序进行排序
http://www.nowcoder.com/practice/dd0c6b26c9e541f5b935047ff4156309
运用了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); } }