【问题标题】:Duplicates in an array not to be printed more than once数组中的重复项不会被多次打印
【发布时间】:2013-03-08 16:39:27
【问题描述】:

下面的代码打印文件中的所有单词(将其放在第一个数组中)和它旁边的第一个单词(第二个数组)。如果单词有重复,它会找到数组中的那个单词(第一个单词)并将 1 添加到数字数组,但它仍会打印出数组中的重复项。我只想要旁边带有正确数字的单词的第一个实例来说明数组中有多少次。我的问题真的是我不希望重复打印出来。 (请不要使用数组列表)。

while ((in.hasNext())) {

    l = in.next() ;

    for(int i = 0; i< Wrd.length-1;i++){
        if (l.equals(Wrd[i])){
            num[i] = num[i] +1;
        } 

    }

    Wrd[n]=l;
    num[n] = num;

    n++;

}

【问题讨论】:

  • 你能把单词放在一组吗?这将强制没有重复。

标签: java arrays


【解决方案1】:

听起来您无法使用 SetMap 等 - 如果可以,那么这里的其他建议更容易实现我的建议 :-)

如果你因为某种原因不能,那么这个怎么样:

// capture all the words first into an array
// the array below is for test purposes
String[] words = {"1", "2", "3", "5", "1", "1", "3", "4", "1", "5", "7", "0"};

Arrays.sort(words);  // sort the array - this is vital or the rest wont work
String last = words[0];
int count = 0;
for (String word : words) {
    if (word.equals(last)) {
        count++;
    } else {
        System.out.println(last + "=>" + count);

        count = 1;
        last = word;
    }
}
System.out.println(last + "=>" + count);

输出将是:

0=>1
1=>4
2=>1
3=>2
4=>1
5=>2
7=>1

【讨论】:

  • 这个解决方案非常有效,肖恩。不幸的是,我花了太多时间试图以艰难的方式解决这个问题。你的帖子帮助很大。谢谢!
【解决方案2】:

使用布尔标志跟踪给定单词是否重复,如果是,则不要将其添加到数组中:

while (in.hasNext()) {
    boolean dup = false;
    l = in.next() ;

    for(int i = 0; i< Wrd.length-1;i++){
        if (l.equals(Wrd[i])){
            num[i] = num[i] +1;
            dup = true;
            break; // No reason to check the rest of the array
        } 
    }

    if (!dup) {
        Wrd[n] = l;
        num[n] = num; // If you're looking for frequency, you probably want 1 not num

        n++; // only increment the index if we add a new word
    }
}

【讨论】:

    【解决方案3】:

    您需要使用地图 - 这是自动处理维护一个唯一的单词列表。如果您覆盖 put 方法来聚合而不是覆盖,那么它将自动累加计数。

    private void readWords(final Iterator<String> in) {
        final Map<String, Integer> wordMap = new HashMap<String, Integer>() {
            @Override
            public Integer put(String key, Integer value) {
                final Integer origValue = get(key);
                if (origValue == null) {
                    return super.put(key, value);
                } else {
                    return super.put(key, origValue + value);
                }
            }
        };
        while (in.hasNext()) {
            wordMap.put(in.next(), 1);
    
        }
        //just for display - not necessary
        for (final Entry<String, Integer> entry : wordMap.entrySet()) {
            System.out.println("Word '" + entry.getKey() + "' appears " + entry.getValue() + " times.");
        }
    }
    

    测试:

    List<String> strings = new LinkedList<String>();
    strings.add("one");
    strings.add("two");
    strings.add("two");
    strings.add("three");
    strings.add("three");
    strings.add("three");
    readWords(strings.iterator());
    

    输出:

    Word 'two' appears 2 times.
    Word 'one' appears 1 times.
    Word 'three' appears 3 times.
    

    您可以使用TreeMap 而不是HashMap 按字母顺序对单词进行排序 - 这可能看起来更适合显示;取决于您打算如何处理地图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-04
      • 1970-01-01
      • 2015-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-18
      相关资源
      最近更新 更多