개체 배열에 개체가 나타나는 위치의 인덱스를 반환하려고합니다.개체 배열을 통해 검색
public static int search(WordCount[] list,WordCount word, int n)
{
int result = -1;
int i=0;
while (result < 0 && i < n)
{
if (word.equals(list[i]))
{
result = i;
break;
}
i++;
}
return result;
}
WordCount[]
은 개체의 배열입니다.
word
은 WordCount
의 인스턴스입니다.
n
는 WordCount[]
그것은 실행의 개체 수 있지만 제대로 인덱스를 반환하지 않습니다. 모든 도움을 주시면 감사하겠습니다. 시간 내 줘서 고마워.
CLASS 사용하는 .equals의 구현() 올바르게 경우 확인되지 않기 때문에이 방법은 아마 작동하지
for (int i=0;i<tokens.length;i++)
{
if (tokens[i].length()>0)
{
WordCount word = new WordCount(tokens[i]);
int foundAt = search(wordList, word, n);
if (foundAt >= 0)
{
wordList[foundAt].increment();
}
else
{
wordList[n]=word;
n++;
}
}
}
}
어떤 결과가 나옵니까? –
'WordCount'에 대한 'equals' 구현을 보여주십시오. – Radiodef
WordCount에서 equals() 및 hashCode() 메서드를 재정의 했습니까? –