题解 | #字符串排序#
字符串排序
https://www.nowcoder.com/practice/5af18ba2eb45443aa91a11e848aa6723
import java.util.Scanner;
import java.util.Arrays;
// 1、注意next与nextLine的区别:next不接受空格和回车,nextLine接受空格和回车,会将回车当做一个字符串
// 2、Arrays.sort(String[] s)字符串数组字典序排序
// 3、不接受多个case
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
int n = in.nextInt();
String[] ss = new String[n];
for (int i = 0; i < n; i++){
ss[i] = in.next(); // 注意,nextLine会接受回车和空格,所以最后的字符串数组会少最后一个字符串,结果会少一个,因此,需要使用next
}
Arrays.sort(ss);
for (String s: ss){
System.out.println(s);
}
}
}
查看10道真题和解析