Java 阵列中的副本不能打印多次

Java 阵列中的副本不能打印多次,java,arrays,Java,Arrays,下面的代码打印文件中的所有单词(将其放入第一个数组)和旁边的数字(第二个数组)。如果有一个单词的副本,它会找到数组中的该单词(第一个),并将1添加到数字数组中,但仍会打印出数组中的副本。我只希望单词的第一个实例旁边有正确的数字,以说明在数组中出现了多少次。我的问题是我不想把副本打印出来。(无阵列列表plz) while((in.hasNext())){ l=in.next(); 对于(int i=0;i

下面的代码打印文件中的所有单词(将其放入第一个数组)和旁边的数字(第二个数组)。如果有一个单词的副本,它会找到数组中的该单词(第一个),并将1添加到数字数组中,但仍会打印出数组中的副本。我只希望单词的第一个实例旁边有正确的数字,以说明在数组中出现了多少次。我的问题是我不想把副本打印出来。(无阵列列表plz)

while((in.hasNext())){
l=in.next();
对于(int i=0;i
您需要使用地图-这会自动处理维护唯一单词列表的问题。如果覆盖
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.");
    }
}

您可以使用
TreeMap
而不是
HashMap
按字母顺序对单词进行排序-这可能更便于显示;取决于您计划对地图执行的操作。

使用布尔标志跟踪给定单词是否重复,如果是:

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
    }
}
while(在.hasNext()中){
布尔dup=假;
l=in.next();
对于(int i=0;i
听起来好像你不能使用
集合
映射
等-如果可以的话,这里的其他建议更容易实现我的建议:-)

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

// 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

你能把这些词排成一组吗?这将不会强制执行重复。此解决方案非常有效,肖恩。不幸的是,我花了太多的时间试图通过艰难的方式解决这个问题。你的帖子帮了大忙。谢谢
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
    }
}
// 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