how to compare elements in a string array in java?

纵然是瞬间 提交于 2019-12-19 11:29:08

问题


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.


回答1:


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;
 }



回答2:


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);



回答3:


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++;
}



回答4:


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());
}

}



回答5:


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();
}


来源:https://stackoverflow.com/questions/12947254/how-to-compare-elements-in-a-string-array-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!