我想在字符串數組中找到重複的單詞。如何比較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)
但不斷給我錯誤的答案。
什麼是字符串數組? – r0ast3d