how to compare elements in a string array in java?

后端 未结 5 1753
予麋鹿
予麋鹿 2021-01-07 12:26

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++)
   {    
             


        
5条回答
  •  半阙折子戏
    2021-01-07 12:45

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

提交回复
热议问题