简体   繁体   English

如何在JAVA中报告列表中最后一个元素的所有并列位置

[英]how to report all tied positions for the last element in the list in JAVA

I have a task to print n frequently used words based on their count values across multiple files. 我的任务是根据多个文件中n个常用单词的计数值来打印它们。 Now the issue is after printing n words, I have to print all the ties at the last position 现在的问题是打印n个单词后,我必须在最后一个位置打印所有领带

for instance if I printed 10 frequently used words based on the highest count and the output comes like this when I use a for loop. 例如,如果我根据最高计数打印了10个常用单词,并且在使用for循环时输出如下。

CODE

    int listSize = newList.size() >= 10 ? 10 : newList.size();

    for (int k = 0; k < listSize; k++) {

        Words w = newList.get(k);

        System.out.println("Word : " + ++j + " " + w.getWord() + " "
                + w.getCount());

        // System.out.println(w.getWord());

    }

OUTPUT : 输出:

word 1 : liked 104
word 2 : hello 98
....
....
....
word 10 : picnic 15

now If I encounter words further with the same count that is 15 I have to print them also if I have five words with the same word count 15 I have to print all of them that is all ties for the last position must be reported like this 现在,如果我再遇到数量相同的单词,即15,则我也必须打印它们,如果我有五个单词的数量与15相同,我也必须打印所有单词,所有与最后位置的联系都必须这样报告

**OUTPUT :**


word 11 : camera  15 
word 12 : monkey  15 
word 13 : carrot  15 
word 14 : penguin 15 
word 15 : bottle  15 

how to implement this case guide me thanks in advance 如何实施这种情况,请事先向我表示感谢

If newList is sorted by getCount then the simpliest way to print words with same count as last printed word is: 如果newListgetCount排序,则打印与最后打印的单词count相同的单词的最简单方法是:

    int k; // init k before the cycle
    for (k = 0; k < listSize; k++) {

        Words w = newList.get(k);

        System.out.println("Word : " + ++j + " " + w.getWord() + " "
                + w.getCount());

        // System.out.println(w.getWord());

    }

    if (k > 0) {
        int lastCount = newList.get(k - 1).getCount(); // last printed count
        // print words from the k-th
        while (k < newList.size()
               && newList.get(k).getCount() == lastCount) {
            Words w = newList.get(k);

            System.out.println("Word : " + ++j + " " + w.getWord() + " "
                    + w.getCount());

            ++k;
        }
    }

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

相关问题 ArrayList仅添加所有位置的最后一个元素 - ArrayList just add the last element for all positions 最后添加的元素将覆盖列表中的所有对象。 爪哇 - The last element added overwrites all the objects in the List. Java 从java集合中的给定列表中删除除最后一个元素之外的所有元素 - Deletes all but the last element from a given list in java collections 串联到Java列表中的最后一个元素 - concatenate to last element in list in java 如何获取Java中最后一个元素之前的所有数组元素 - How To Get All Element of Array before last one in Java 有一个Java数组列表; 如何按每个数组的最后一个元素排序? - Have a List of Arrays in Java; How to Sort By the Last Element of Each Array? RecyclerView正在使用我所有列表中的最后一个元素进行填充 - RecyclerView is populating with the last element all my list 如何在最后一个元素处停止列表? - How to stop the list at the last element? 是否可以在Java中编写一个递归方法,将列表中的所有项目都替换为最后一个元素?(不使用循环) - Is it possible to write a recursive method in java where all items in a list is replaced by the last element?(without using loops) 从Java util列表读取时,为什么所有元素都返回最后一个元素? (概括) - When reading from a java util List, why do all elements return the last element? (generalized)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM