简体   繁体   English

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

[英]How to sort a List<String> according to the length of the strings and print the first N elements

Your program should read an input file (the first argument to your program). 您的程序应该读取输入文件(程序的第一个参数)。 The first line contains the value of the number 'N' followed by multiple lines. 第一行包含数字'N'的值,后跟多行。 You may assume that the input file is formatted correctly and the number on the first line ie 'N' is a valid positive integer.eg 您可以假设输入文件格式正确,第一行的数字即'N'是有效的正整数.eg

This is my Input: 这是我的输入:

2 2
Hello World 你好,世界

CodeEval CodeEval
Quick Fox 快狐
A 一种
San Francisco 旧金山

Desired Output should be: 期望的输出应该是:

San Francisco 旧金山
Hello World 你好,世界

This is my code: 这是我的代码:

 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"));
    System.out.println("Number of lines to be printed : "+s);
    System.out.println("After removing first element of ArrayList : "+myList);
    System.out.println("Size of the ArrayList : "+myList.size());               

    Comparator comparator=Collections.reverseOrder();                   
    Collections.sort(myList,comparator);
    System.out.println("After sorting ArrayList in Descending Order :"+myList);


    int x = Integer.parseInt(s);
    System.out.println(System.getProperty("line.separator"));


for (String s1 : myList) {
System.out.println(s1);
}
System.out.println(System.getProperty("line.separator"));

for(int i=0; i<x; i++){
        System.out.println(myList.get(i));
    }

}   

}

But i am getting this output: 但我得到这个输出:

San Francisco 旧金山
Quick Fox 快狐

Where am i going wrong? 我哪里错了?

Tje default sorting, will sort the list according to the alphabetical index. 默认排序,将根据字母索引对列表进行排序。 If you want to sort for other criterias, like the length in your case, you must implement oyur own Comparator 如果你想对其他标准进行排序,比如你的长度,你必须实现oyur自己的Comparator

    Comparator<String> x = new Comparator<String>()
    {
        @Override
        public int compare(String o1, String o2)
        {
            if(o1.length() > o2.length())
                return -1;

            if(o2.length() > o1.length())
                return 1;

            return 0;
        }
    };

    Collections.sort(mylist,  x);

In your code, You just remove the first item in your list (myList). 在您的代码中,您只需删除列表中的第一项(myList)。 The size of the list will then be 4. Then you soort the list in reverse order zo it's normal you get the output: 然后列表的大小为4.然后以相反的顺序排列列表,这是正常的,你得到输出:

San Francisco
Quick Fox
Hello World
CodeEval
A

As far as I can see you just removed your first element so your output would contain 5 elements. 据我所知,你刚删除了你的第一个元素,所以你的输出将包含5个元素。 I don't see why you would want to get the desired output. 我不明白为什么你想得到所需的输出。 The output is reversed alphabetical. 输出按字母顺序反转。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当 n &gt; list index-1 时,打印字符串列表中前 n 个元素的计数器和字符串,使用默认值 - Print counter and string for first n elements from list of strings, with default value, when n > list index-1 如何根据int打印数组的第一个元素? - How to print the first elements of an array according to an int? 如何随机打印 10 个带有“n”个字母的字符串,但每个字符串都有相同的第一个字母 - How would one be able to randomly print 10 strings with 'n' letters, but each string has the same first letter 如何按长度对字符串排序 - How to sort strings by length 如何根据元素的总和对包含整数列表的列表进行排序 - How to sort a list containing a list of integers according to the sum of their elements 如何排序字符串列表,其中先出现的字符串是startWith,然后是endWith - How to sort list of strings where first comes strings with startWith then endWith 根据另一个列表字符串对列表字符串进行排序 - Sort list string according to another list string 如何对列表进行排序 <Date, String> 在Java中根据Date - how to sort the list<Date, String> in Java according to Date 如何根据存储的双倍分数对字符串列表进行排序? - How to sort a List of String according to a Double score that is stored? 根据特定的数组元素对数组进行排序 - Sort List of arrays according to specific array elements
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM