2012-10-18 149 views
1

我想在字符串數組中找到重複的單詞。如何比較java中的字符串數組中的元素?

這裏是我比較代碼:if語句

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。這是什麼意思?有一個更好的方法嗎?我只是試着做

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

但不斷給我錯誤的答案。

+0

什麼是字符串數組? – r0ast3d

回答

0

NullPointerException異常意味着你的陣列成員之一未設置(即它爲空)

不要使用==來比較字符串。

你是在正確的軌道上 - 機會是stringArray[]包含一些未設置的成員。 Eacy修復是在使用這些值之前進行空檢查。

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

你怎麼空檢查? – Katherine

0

這意味着stringArray[i]null,即你的數組中有一個null進入的地方。有可能您在其他地方出現邏輯錯誤,並且陣列的某些元素未被正確設置。

如果陣列合法包含空值,你必須設法呼籲stringArray[i]方法之前明確檢查此:

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

謝謝,但如果它爲空,我如何逃避for循環? – Katherine

+0

爲什麼我的數組有空值?這裏是我用來設置字符串數組的代碼: String [] stringArray = new String [wordCount]; 而 { 對(INT K = 0; k <的wordCount; k ++)(line.equals( 「DONE」)!) { //把令牌成字符串數組 StringTokenizer的令牌=新的StringTokenizer(線); stringArray [k] = tokens.nextToken(); } } – Katherine

+0

@Katherine我猜是因爲'line'最初是''DONE'',所以你的初始化循環從不執行?另外,如果'line'不是''DONE'',那麼循環將永遠運行,因爲在循環體中'line'沒有被更新,並且你應該使用'String.split(「\\ s」)'代替'StringTokenizer'。 – verdesmarald

1

您可以尤爲明顯的表現這樣做:

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

空指針可能是因爲你的數組中有任何空值。

您的代碼不工作,因爲你是在你需要查找重複

你可以用下面的代碼在陣列來算的話重複同一陣列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()); 
} 

} 
0

在這裏,我看到你正在試圖找到給定字符串的唯一元素計數。我會建議使用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(); 
} 
相關問題