CoreJava/exp8/CollectionSort.java

37 lines
997 B
Java
Raw Permalink Normal View History

2024-05-09 09:10:51 +08:00
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<Integer> 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+" ");
}
}
}