how to compare elements in a string array in java?

2019-02-20 14:45发布

I am trying to find duplicate words in a string array.

Here is my code for the comparison:

   for ( int j = 0 ; j < wordCount ; j++)
   {    
       for (int i = wordCount-1 ; i > j ; i--)
       {       
           if (stringArray[i].compareTo(stringArray[j]) == 0 && i!=j)
           {
               //duplicate
               duplicates++;
           }
       }
   }
   wordCount -= duplicates;
   System.out.print("\nNumber of words, not including duplicates: " + wordCount);

in the if statement, it says NullPointerException. What does this mean? Is there a better way to do this? I tried simply doing

if (stringArray[i] == stringArray[j] && i!=j)

but that kept giving me wrong answers.

5条回答
在下西门庆
2楼-- · 2019-02-20 14:50

You can do like this for beter performance:

public int getDuplicateCount(Integer[] arr){
     int count = 0;   
     Set<Integer> set = new HashSet<Integer>();
     for (int i = 0; i < arr.length; i++) {
         if (set.contains(arr[i]))
             count++;
         set.add(arr[i]);
      }
      return count;
 }
查看更多
疯言疯语
3楼-- · 2019-02-20 14:50

It means stringArray[i] is null, i.e. your array has a null entry in it somewhere. It's possible that you have a logic error elsewhere and some elements of the array are not being set correctly.

If your array legitimately contains nulls, you have to explicitly check for this before trying to call methods on stringArray[i]:

if (stringArray[i] == null){
    // Do whatever
} else if (stringArray[i].compareTo(stringArray[j]) == 0 && i!=j) {
    //duplicate
    duplicates++;
}
查看更多
Animai°情兽
4楼-- · 2019-02-20 15:03

Here i see you are trying to find unique elements count for given string. I would suggest using HashSet for better solution.

public int getUniqueElements(String str)
{
  HashSet<Character> hSet = new HashSet<>();

  // iterate given string, hSet only adds unique elements to hashset
  for(int i = 0; i < str.length() ; i++
    hSet.add(str.charAt(i));

  return hSet.size();
}
查看更多
虎瘦雄心在
5楼-- · 2019-02-20 15:05

NullPointerException means that one of your array members is not set (i.e. it is null)

Don't use == to compare strings.

You are on the right track - chances are stringArray[] contains some members that are not set. Eacy fix is to null check before using the values.

for ( int j = 0 ; j < wordCount ; j++)
   {    
       for (int i = wordCount-1 ; i > j ; i--)
       {       
           String wordi = stringArray[i];
           String wordj = strinArray[j];
           // If both are null it won't count as a duplicate.
           // (No real need to check wordj - I do it out of habit)
           if (wordi != null && wordj != null && wordi.compareTo(wordj) == 0 && i!=j)
           {
               //duplicate
               duplicates++;
           }
       }
   }
   wordCount -= duplicates;
   System.out.print("\nNumber of words, not including duplicates: " + wordCount);
查看更多
我欲成王,谁敢阻挡
6楼-- · 2019-02-20 15:11

Null pointer may be because you have any null value in your array.

Your code is not working because you are itrating on same array on which you need to find duplicates

you can use following code to count duplicate words in array.

public class WordCount {


public static void main(String args[]){
    String stringArray[]={"a","b","c","a","d","b","e","f"};

    Set<String> mySet = new HashSet<String>(Arrays.asList(stringArray));

    System.out.println("Number of duplicate words: "+ (stringArray.length -mySet.size()));

    System.out.println("Number of words, not including duplicates: "+ mySet.size());
}

}
查看更多
登录 后发表回答