【问题标题】:Delete duplicates in array, Java [duplicate]删除数组中的重复项,Java [重复]
【发布时间】:2014-04-23 13:27:03
【问题描述】:

我写了一个方法来计算单词在 word 文件中出现的次数。之前,在另一种方法中,我对单词进行了排序,以按字母顺序显示。该方法的示例输入将如下所示: 是 离开 鸟类 鸟类 去 去 有

我的问题是..如何删除此方法中的重复出现? (在计算 ofcoz 之后)我尝试使用另一个字符串数组将唯一的数组复制到该字符串数组中,但我得到一个空指针异常。

public static String[] counter(String[] wordList)
{
    for (int i = 0; i < wordList.length; i++) 
    {
         int count = 1;
         for(int j = 0; j < wordList.length; j++)
         {
             if(i != j)  //to avoid comparing itself
             {
                 if (wordList[i].compareTo(wordList[j]) == 0)   
                 {
                     count++;
                 }
             }
         }

         System.out.println (wordList[i] + " " + count);


     }

    return wordList; 
}

任何帮助将不胜感激。

哦,我当前的输出看起来像这样: 是 1 客场 1 鸟 2 鸟 2 去 2 去 2 有 1 个

【问题讨论】:

  • 将其转换为“集合”。集合只能包含唯一元素。
  • "我尝试使用另一个字符串数组将唯一的字符串数组复制到该字符串数组中,但我得到一个空指针异常" - 所以你放弃了这种方法,因为空指针异常?听起来对我来说是一种有效的方法
  • @AnthonyAccioly 不,那不是我。但是谢谢,我也会经历这个。..
  • @leo 我没有放弃。我只是在寻找另一种方法。
  • 请看看我的回答,让我知道它对你有什么作用。

标签: java arrays string counter


【解决方案1】:

我更喜欢使用 Map 来存储单词的出现。地图中的键存储在 Set 中,因此无法复制。这样的事情怎么办?

public static String[] counter(String[] wordList) {
    Map<String, Integer> map = new HashMap<>();

    for (int i = 0; i < wordList.length; i++) {
        String word = wordList[i];

        if (map.keySet().contains(word)) {
            map.put(word, map.get(word) + 1);
        } else {
            map.put(word, 1);
        }
    }

    for (String word : map.keySet()) {
        System.out.println(word + " " + map.get(word));
    }

    return wordList;
}

【讨论】:

    【解决方案2】:

    我已经在this question 上发布了答案。您的问题几乎相同 - 他在创建另一个数组和获得 NPE 时遇到了问题。

    这是我想出的(假设数组已排序):

    public static String[] noDups(String[] myArray) { 
    
        int dups = 0; // represents number of duplicate numbers
    
        for (int i = 1; i < myArray.length; i++) 
        {
            // if number in array after current number in array is the same
            if (myArray[i].equals(myArray[i - 1]))
                dups++; // add one to number of duplicates
        }
    
        // create return array (with no duplicates) 
        // and subtract the number of duplicates from the original size (no NPEs)
        String[] returnArray = new String[myArray.length - dups];
    
        returnArray[0] = myArray[0]; // set the first positions equal to each other
                                     // because it's not iterated over in the loop
    
        int count = 1; // element count for the return array
    
        for (int i = 1; i < myArray.length; i++)
        {
            // if current number in original array is not the same as the one before
            if (!myArray[i].equals(myArray[i-1])) 
            {
               returnArray[count] = myArray[i]; // add the number to the return array
               count++; // continue to next element in the return array
            }
        }
    
        return returnArray; // return the ordered, unique array
    }
    

    示例输入/输出:

    String[] array = {"are", "away", "birds", "birds", "going", "going", "has"};
    
    array = noDups(array);
    
    // print the array out
    for (String s : array) {
        System.out.println(s);
    }
    

    输出:

    are
    away
    birds
    going
    has
    

    【讨论】:

    • 谢谢,这真的帮助解决了我遇到的问题。
    猜你喜欢
    • 2014-04-16
    • 2023-03-20
    • 2012-04-20
    • 2011-06-29
    • 1970-01-01
    • 2015-11-07
    • 2013-03-23
    • 2013-08-03
    相关资源
    最近更新 更多