小编AB2*_*B25的帖子

如何根据字符串的长度对List <String>进行排序并打印前N个元素

您的程序应该读取输入文件(程序的第一个参数).第一行包含数字'N'的值,后跟多行.您可以假设输入文件格式正确,第一行的数字即'N'是有效的正整数.eg

这是我的输入:

2
Hello World

CodeEval
快速福克斯
一个
旧金山

期望的输出应该是:

旧金山
你好世界

这是我的代码:

 class Longest
 {
public static void main(String args[]) throws FileNotFoundException  
{
    BufferedReader in = null;
    List<String> myList = new ArrayList<String>();
    try 
    {   
        in = new BufferedReader(new FileReader("C:\\filename.txt"));
        String str;
        while ((str = in.readLine()) != null) 
        {
            if (str.length() == 0) continue;                                

            myList.add(str);
        }
    } 
    catch (IOException e) 
    {
        e.printStackTrace();
    } 
    System.out.println("Contents of the ArrayList : "+myList);
    System.out.println("Size of the ArrayList : "+myList.size());
    String s = myList.remove(0);
    System.out.println(System.getProperty("line.separator")); …
Run Code Online (Sandbox Code Playgroud)

java string arraylist

2
推荐指数
1
解决办法
8348
查看次数

标签 统计

arraylist ×1

java ×1

string ×1