題目描述
給定n個字符串,請對n個字符串按照字典序排列。
輸入描述:
輸入第一行為一個正整數n(1≤n≤1000),下面n行為n個字符串(字符串長度≤100),字符串中只含有大小寫字母。
輸出描述:
數據輸出n行,輸出結果為按照字典序排列的字符串。
輸入例子:
9 cap to cat card two too up boat boot
輸出例子:
boat boot cap card cat to too two up
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int n = Integer.valueOf(sc.nextLine()); List<String> list = new ArrayList(); for(int i = 0; i < n; i++){ list.add(sc.nextLine()); } Collections.sort(list); Iterator iter = list.iterator(); while(iter.hasNext()){ System.out.println(iter.next()); } } sc.close(); } }