简体   繁体   English

如何在Java中比较字符串数组中的元素?

[英]how to compare elements in a string array in java?

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 . 在if语句中,它表示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. 但这一直给我错误的答案。

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

NullPointerException means that one of your array members is not set (ie it is null) NullPointerException表示未设置您的数组成员之一(即为null)

Don't use == to compare strings. 不要使用==比较字符串。

You are on the right track - chances are stringArray[] contains some members that are not set. 您处在正确的轨道上stringArray[]包含一些未设置的成员。 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);

It means stringArray[i] is null , ie your array has a null entry in it somewhere. 这意味着stringArray[i]null ,即您的数组中某处有一个null条目。 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] : 如果您的数组合法包含null,则必须在尝试调用stringArray[i]上的方法之前显式检查此内容:

if (stringArray[i] == null){
    // Do whatever
} else if (stringArray[i].compareTo(stringArray[j]) == 0 && i!=j) {
    //duplicate
    duplicates++;
}

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

}

Here i see you are trying to find unique elements count for given string. 在这里,我看到您正在尝试查找给定字符串的唯一元素计数。 I would suggest using HashSet for better solution. 我建议使用HashSet以获得更好的解决方案。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM