Java 在对象数组中搜索

Java 在对象数组中搜索,java,arrays,search,return,Java,Arrays,Search,Return,我试图返回对象在对象数组中出现的位置的索引 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;

我试图返回对象在对象数组中出现的位置的索引

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;
}
我怎么称呼它

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++;
                }
            }
        }
    }
for(int i=0;i0)
{
WordCount word=新的WordCount(标记[i]);
int foundAt=search(单词列表,单词,n);
如果(foundAt>=0)
{
单词表[foundAt].increment();
}
其他的
{
单词表[n]=单词;
n++;
}
}
}
}

此方法可能不起作用,因为您使用的.equals()的实现未正确检查两个对象是否相等


您需要覆盖WordCount对象的equals()和hashCode()方法,或者让它返回您想要比较的内容,例如:
word.getWord().equals(list[i].getWord())
使用起来似乎更容易:

public static int search(WordCount[] list, WordCount word)
{
    for(int i = 0; i < list.length; i++){
        if(list[i] == word){
            return i;
        }
    }
    return -1;
}
publicstaticint搜索(WordCount[]列表,WordCount单词)
{
for(int i=0;i

这将检查数组中的每个值,并将其与指定的单词进行比较。

当前方法中的奇怪之处在于,您必须创建一个新的
WordCount
对象以查找特定单词的计数。您可以添加如下方法

public boolean hasEqualWord(WordCount other)
{
    return word.equals(other.word);
}
WordCount
类中,并使用它而不是
equals
方法:

....
while (result < 0 && i < n)
{
    if (word.hasEqualWord(list[i])) // <--- Use it here!
    { 
    ....
    }
}
默认情况下,只返回两个引用是否引用同一对象(与
=
操作符相同)。查看您正在做的事情,您需要做的是在
WordCount
中创建一个返回
word
的方法,例如:

public String getWord() {
    return word;
}
然后在
搜索中更改比较:

if (word.equals(list[i]))
致:

或者更改方法的签名以接受
字符串
,这样,如果不需要,就不会创建新对象

我不建议在
WordCount
中重写
equals
,这样它就只使用
word
来确定对象的相等性,因为您还有其他字段。(例如,也可以认为两个计数器只有在计数相同时才相等。)

另一种方法是使用
映射
,它是一个关联容器。例如:

public static Map<String, WordCount> getCounts(String[] tokens) {
    Map<String, WordCount> map = new TreeMap<String, WordCount>();

    for(String t : tokens) {
        WordCount count = map.get(t);
        if(count == null) {
            count = new WordCount(t);
            map.put(t, count);
        }

        count.increment();
    }

    return map;
}
publicstaticmap getCounts(字符串[]标记){
Map Map=newtreemap();
for(字符串t:令牌){
WordCount=map.get(t);
如果(计数=null){
计数=新单词计数(t);
map.put(t,count);
}
count.increment();
}
返回图;
}

它返回什么?向我们展示您的
WordCount
equals
实现。您是否覆盖了WordCount中的equals()和hashCode()方法?它应该返回
WordCount
中找到
WordCount
的位置的索引。根据注释,您已经创建了自己的类
WordCount
,但是您没有重写
equals
方法。发布你的
WordCount
类,你会很快得到帮助。这不起作用,WordCount无法与==运算符或Anks man进行比较。我不知道这就像在我的类中抛出一个getWord函数那么简单。在过去的三个小时里,我一直在为这个问题焦头烂额。但由于某种原因,我的增量不起作用?当我打印输出时,它只为所有单词打印一(1)。知道我哪里做错了吗?我是个白痴,发现了我的问题。我真的很感谢你的帮助
public String getWord() {
    return word;
}
if (word.equals(list[i]))
if (word.getWord().equals(list[i].getWord()))
public static Map<String, WordCount> getCounts(String[] tokens) {
    Map<String, WordCount> map = new TreeMap<String, WordCount>();

    for(String t : tokens) {
        WordCount count = map.get(t);
        if(count == null) {
            count = new WordCount(t);
            map.put(t, count);
        }

        count.increment();
    }

    return map;
}