java 如何获取ArrayList中元素的位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39318754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 04:15:40  来源:igfitidea点击:

How to get position of element in ArrayList

javaarraylist

提问by sldroid

I used following method to add my data to ArrayList.

我使用以下方法将我的数据添加到 ArrayList。

ArrayList<Word> wordList = new ArrayList<Word>();
Word word = new Word();
word.set_id(id);
word.setWord(word);
word.setDefinition(definition);
wordList.add(word);

After the add some data, I want find the position of the any id which I want find in ArrayList.

添加一些数据后,我想找到我想在 ArrayList 中找到的任何 id 的位置。

Already I have tried following method to get position by id. But it isn't work.

我已经尝试使用以下方法通过 id 获取位置。但它不起作用。

int position = wordList.indexOf(id);

and

int position = wordList.lastIndexOf(id);

Both codes always generated "position = -1" as a result. How can I do that?

结果这两个代码总是生成“position = -1”。我怎样才能做到这一点?

Edited

已编辑

This is the code of the Word.java class. How can I implement equal method?

这是 Word.java 类的代码。如何实现equal方法?

public class Word {
    private String _id, word, definition, favourite, usage;

    public String get_id() {
        return _id;
    }

    public void set_id(String _id) {
        this._id = _id;
    }

    public String getWord() {
        return word;
    }

    public void setWord(String word) {
        this.word = word;
    }

    public String getDefinition() {
        return definition;
    }

    public void setDefinition(String definition) {
        this.definition = definition;
    }

    public String getFavourite() {
        return favourite;
    }

    public void setFavourite(String favourite) {
        this.favourite = favourite;
    }

    public String getUsage() {
        return usage;
    }

    public void setUsage(String usage) {
        this.usage = usage;
    }
}

回答by OneCricketeer

indexOfis trying to compare Word objects. Your list doesn't contain ids as the elements, so you get -1.

indexOf正在尝试比较 Word 对象。您的列表不包含 id 作为元素,因此您会得到 -1。

You need to use a loop and search the list.

您需要使用循环并搜索列表。

int id = 3; 
int position = -1;
for (int i = 0; i < wordlist.size(); i++) {
    if (wordlist.get(i).getId() == id) {
        position = i;
        // break;  // uncomment to get the first instance
    }
} 

Note: this will search the whole list to find the last index of that id. So if there are duplicates and you only want the first one (or stop the loop as soon as you find what you want) add a breakin the if statement.

注意:这将搜索整个列表以找到该 id 的最后一个索引。因此,如果存在重复项并且您只想要第一个(或在找到所需内容后立即停止循环)break,请在 if 语句中添加一个。

回答by Ankit Bhatnagar

  1. Implement equals method in the "Word" object. Inside equals method you can apply equals only to id field.

  2. Create a new Word object with that id and pass that object in indexOf. Don't pass the id in the indexOf. Pass the new of existing Word object with the required id.

  1. 在“Word”对象中实现 equals 方法。在 equals 方法中,您只能将 equals 应用于 id 字段。

  2. 使用该 id 创建一个新的 Word 对象,并在 indexOf 中传递该对象。不要在 indexOf 中传递 id。传递具有所需 ID 的现有 Word 对象的新对象。

Then indexOf will return the valid index of this word object.

然后 indexOf 将返回这个单词对象的有效索引。

回答by Rajadurai K

For searching the object in a list. you need to override equals method in your Word class. otherwise you will get -1. because indexOf internally used equals method to search the element in list.

用于搜索列表中的对象。您需要覆盖 Word 类中的 equals 方法。否则你会得到-1。因为 indexOf 内部使用了 equals 方法来搜索列表中的元素。

回答by YMomb

The class inside your list should implement hascode() and equals() in order to have indexOf() that works.

列表中的类应该实现 hascode() 和 equals() 以使 indexOf() 起作用。