package exp8; import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; public class CollectionSort { public static void main(String[] args) { //获取以','分割数字的字符串, 以"-1"结束 Scanner scan = new Scanner(System.in); System.out.println("输入以','分割的数字, 以\"-1\"结束"); String str = scan.nextLine(); scan.close(); //分割字符串 String[] sNums = str.split(","); //将字符串数组转为整形数组 ArrayList nums = new ArrayList<>(); for (String s : sNums) { if(s.equals("-1")) break; else nums.add(Integer.parseInt(s)); } //对整形数组进行排序 Collections.sort(nums); //输出整形数组 System.out.println("\n排序后的数组:"); for (Integer num : nums) { System.out.print(num+" "); } } }