字符串按照字典序排列

题目描述

给定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
思路:将字符串保存在ArrayList数组中,使用Collections.sort(数组名)排序
 1 import java.util.*;
 2 
 3 public class Main{
 4     public static void main(String[] args){
 5         Scanner sc=new Scanner(System.in);
 6         while(sc.hasNext()){
 7             int n=Integer.valueOf(sc.nextLine());
 8             List<String> list=new ArrayList<>();
 9             for(int i=0;i<n;i++){
10                 list.add(sc.nextLine());
11             }
12             Collections.sort(list);
13             Iterator iter=list.iterator();
14             while(iter.hasNext()){
15                 System.out.println(iter.next());
16             }
17         }
18         sc.close();
19     }
20 }

 

posted @ 2017-04-07 10:20  临江仙zhe  阅读(350)  评论(0)    收藏  举报