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

如何在java中比较字符串数组中的元素?,java,arrays,string,nullpointerexception,compareto,Java,Arrays,String,Nullpointerexception,Compareto,我试图在字符串数组中找到重复的单词 以下是我的比较代码: for ( int j = 0 ; j < wordCount ; j++) { for (int i = wordCount-1 ; i > j ; i--) { if (stringArray[i].compareTo(stringArray[j]) == 0 && i!=j) {

我试图在字符串数组中找到重复的单词

以下是我的比较代码:

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

但是这一直给了我错误的答案。

NullPointerException表示您的一个数组成员未设置(即,它为null)

不要使用==来比较字符串

您的做法是正确的-可能
stringArray[]
包含一些未设置的成员。每个修复程序在使用值之前都要进行空检查

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);
for(int j=0;jj;i--)
{       
String-wordi=stringArray[i];
字符串wordj=strinArray[j];
//如果两者都为空,则不计算为重复。
//(没有必要检查wordj——我这样做是出于习惯)
如果(wordi!=null&&wordj!=null&&wordi.compareTo(wordj)==0&&i!=j)
{
//复制品
复制++;
}
}
}
字数-=重复项;
System.out.print(“\n字数,不包括副本:“+wordCount”);

NullPointerException表示未设置一个数组成员(即,它为null)

不要使用==来比较字符串

您的做法是正确的-可能
stringArray[]
包含一些未设置的成员。每个修复程序在使用值之前都要进行空检查

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);
for(int j=0;jj;i--)
{       
String-wordi=stringArray[i];
字符串wordj=strinArray[j];
//如果两者都为空,则不计算为重复。
//(没有必要检查wordj——我这样做是出于习惯)
如果(wordi!=null&&wordj!=null&&wordi.compareTo(wordj)==0&&i!=j)
{
//复制品
复制++;
}
}
}
字数-=重复项;
System.out.print(“\n字数,不包括副本:“+wordCount”);

意思是
stringArray[i]
null
,即数组中的某个地方有一个
null
条目。您可能在其他地方有逻辑错误,并且数组的某些元素设置不正确

如果您的数组合法地包含空值,则在尝试调用
stringArray[i]
上的方法之前,必须显式检查此值:

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

这意味着
stringArray[i]
null
,即数组中某处有一个
null
条目。您可能在其他地方有逻辑错误,并且数组的某些元素设置不正确

如果您的数组合法地包含空值,则在尝试调用
stringArray[i]
上的方法之前,必须显式检查此值:

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

为了获得更好的性能,您可以这样做:

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;
 }
public int getDuplicateCount(整数[]arr){
整数计数=0;
Set=newhashset();
对于(int i=0;i
为了获得更好的性能,您可以这样做:

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;
 }
public int getDuplicateCount(整数[]arr){
整数计数=0;
Set=newhashset();
对于(int i=0;i
空指针可能是因为数组中有任何空值

您的代码不起作用,因为您正在同一个数组上执行itrating,您需要在该数组上查找重复项

您可以使用以下代码对数组中的重复字进行计数

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

}
公共类字数{
公共静态void main(字符串参数[]){
字符串字符串数组[]={“a”、“b”、“c”、“a”、“d”、“b”、“e”、“f”};
Set mySet=newhashset(Arrays.asList(stringArray));
System.out.println(“重复字数:+(stringArray.length-mySet.size());
System.out.println(“字数,不包括重复项:+mySet.size());
}
}

空指针可能是因为数组中有任何空值

您的代码不起作用,因为您正在同一个数组上执行itrating,您需要在该数组上查找重复项

您可以使用以下代码对数组中的重复字进行计数

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

}
公共类字数{
公共静态void main(字符串参数[]){
字符串字符串数组[]={“a”、“b”、“c”、“a”、“d”、“b”、“e”、“f”};
Set mySet=newhashset(Arrays.asList(stringArray));
System.out.println(“重复字数:+(stringArray.length-mySet.size());
System.out.println(“字数,不包括重复项:+mySet.size());
}
}

在这里,我看到您正在尝试查找给定字符串的唯一元素计数。我建议使用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();
}
public int-getUniqueElements(字符串str)
{
HashSet hSet=新的HashSet();
//迭代给定的字符串,hSet只向hashset添加唯一的元素
对于(int i=0;i
在这里,我看到您正在尝试查找给定字符串的唯一元素计数。我建议使用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();
}
public int-getUniqueElements(字符串str)
{
HashSet hSet=新的HashSet();
//迭代给定的字符串,hSet只向hashset添加唯一的元素
对于(int i=0;i
string数组中有什么?string数组中有什么?为什么我的数组中有空值?下面是我用来设置字符串数组的代码:string[]stringArray=new string[wordCount];而(!line.equals(“DONE”){for(int k=0;k