题解 | #字符串排序#
字符串排序
https://www.nowcoder.com/practice/5af18ba2eb45443aa91a11e848aa6723
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if(n<1 || n>1000){
return;
}
List<String> strList = new ArrayList<>(n);
while(n > 0){
String str = sc.next(); //此处nextLine()通过不了,因为next()不接受空格和回车
int len = str.length();
if(len==0 || len>100 || str==""){
return;
}
strList.add(str);
n--;
}
Collections.sort(strList);
strList.forEach(s -> {
System.out.println(s);
});
}
}