【问题标题】:Tree/Hash map with LList printout带有列表打印输出的树/哈希图
【发布时间】:2013-03-05 12:38:11
【问题描述】:

我的程序接收一个文本文件并将每个唯一的单词(或字符组)存储为映射中的键,并存储每个单词出现的行号的链接列表。我还在 printEntry 方法中实现了一个发生计数器。

我的问题是,如果一个单词在一行上出现多次,我会尽量避免打印相同的行号两次。我在 printEntry 方法中使用了 if 语句,似乎已经接近了,但仍然没有雪茄。我不想阻止将重复的行号添加到列表中,因为它仍然需要被计算以增加出现变量。

这是一个会给我带来麻烦的输入:

keyboard
mouse mouse
mouse

我需要输出如下所示:

ID: keyboard  Line Numbers: 1  Occurance: 1
ID: mouse  Line Numbers: 2,3  Occurance 3

我现在只提供 printEntry 方法以保持帖子简短。如果需要,我可以提供更多代码。谢谢。

public static void printEntry(Map.Entry entry){

    //local occurance variable
    int occurance = 1;

    //print the word and the line numbers as well as test for duplicate line integers on the same key
    Iterator itr = ((LinkedList) entry.getValue()).iterator();
    System.out.print("ID: " + entry.getKey() + "   Lines: " + itr.next());

    //object variable to store previous line number
    Object check = itr.next();
    while(itr.hasNext()){
        occurance++;
        if (check != itr.next()){
            System.out.print(", " + itr.next());
        }
        else {
            System.out.println("Skipped duplicate");
        }
    }
    //prints occurance from incremented occurance variable
    System.out.print("  " + " Occurance: " + occurance);
    System.out.println();
}

编辑-

我希望所有条目的信息都显示在同一行,因为我们将要扫描大型 (r) 文档。我已将 printEntry 方法的格式设置为接近我想要的位置,但无法弄清楚如何使用 for 循环进行操作。

        public void printEntry(Map.Entry<String, WordStats> entry) {
    String word = entry.getKey();
    WordStats stats = entry.getValue();

    System.out.print("ID: " + word + "  Occurrences: " 
                       + stats.getOccurrences() + " Lines: ");
    for (Integer lineNumber : stats.getLines()) {
        System.out.println(lineNumber);
    }
}

【问题讨论】:

    标签: java list dictionary key


    【解决方案1】:

    所以你想要,对于每个单词,保留

    • 出现的次数
    • 出现的一组已排序的行号(通过集合,我的意思是没有重复的行号)

    那就这样做吧:

    public class WordStats {
        private int occurrences;
        private SortedSet<Integer> lineNumbers = new TreeSet<Integer>();
    
        public void addOccurrence(int lineNumber) {
            occurrences++; 
            lineNumbers.add(lineNumber);
        }
    
        // getters ommitted for brevity
    }
    

    现在只需使用Map&lt;String, WordStats&gt;。对于文本中的每个单词,如果它还没有在地图中,则添加一个 WordStats,并将其添加到其 WordStats 实例中。

    printEntry 方法如下所示:

    public void printEntry(Map.Entry<String, WordStats> entry) {
        String word = entry.getKey();
        WordStats stats = entry.getValue();
        System.out.println("The word " + word + " has been met " 
                           + stats.getOccurrences() + " time(s), on the following line(s):");
        for (Integer lineNumber : stats.getLines()) {
            System.out.println(lineNumber);
        }
    }
    

    【讨论】:

    • 我什至不知道如何感谢你。我在南加州大学(南卡罗来纳州)的教授每学期支付 1500 美元,他花了几个小时向我解释,但我仍然不明白。你在 30 秒内就完成了。
    • 如果你愿意,可以给我 1500 美元 ;-)
    • 大声笑。不幸的是,他不提供退款。你能看看我的编辑并给我你的想法吗?
    • 将 System.out.println(...) 替换为 System.out.print(...)。并且,在循环之后,添加System.out.println(); 以打印行尾。查看java.lang.System 的javadoc。检查其out 属性的类型。点击这个类型可以查看它除了println()之外还有哪些方法。
    • 再次感谢。我只是在 for 循环中添加了一个 if 语句来检查它是否是第一个值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-10
    • 2023-03-31
    • 2015-12-21
    • 1970-01-01
    • 2018-04-17
    • 1970-01-01
    • 2019-02-03
    相关资源
    最近更新 更多