Java 数组列表索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12238674/
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-10-31 08:09:00  来源:igfitidea点击:

Java arraylist indexof

javaarraylistindexing

提问by kibar

Here is my class animals.java:

这是我的课animals.java

public class animals {
    String Name, ID;
    static ArrayList<animals> animalData = new ArrayList<animals>();

    public animals(){}
    public animals(String name, String id){
        super();
        this.Name = name;
        this.ID = id;
    }

    public void addAnimal(String name, String id){
        animalData.add(new animals(name, id));
    }

    public int search(String name){
        return this.animalData.indexOf(name);
    }
}

When I add an animal name with an id it works normally, but when I use search method I saw only -1. Maybe I try override method equalsor indexofin this class? help me for this

当我添加带有 id 的动物名称时,它可以正常工作,但是当我使用搜索方法时,我只看到了 -1。也许我尝试覆盖方法equalsindexof在这个类中?帮我解决这个问题

Thank you and sorry for my bad english..

谢谢你,对不起我的英语不好..

回答by kosa

Yes, you need to override equals()and hashcode()methods when you use objects in collections and perform lookup based on object.

是的,当您在集合中使用对象并基于对象执行查找时,您需要覆盖equals()hashcode()方法。

indexOf()returns object because it just returns object at that perticular index. But, when you do object based lookup, if equals() and hashCode()are not overridden, equals()may fail and you get unpredictable results.

indexOf()返回对象,因为它只返回该特定索引处的对象。但是,当您进行基于对象的查找时,如果 equals() 并且hashCode()没有被覆盖,则equals()可能会失败并且您会得到不可预测的结果。

回答by unholysampler

You are adding instances of animalsto the list. You are searching for the instance by name. Since animalDatadoes not contain any instances of String, indexOf()will never return an index.

您正在将 的实例添加animals到列表中。您正在按名称搜索实例。由于animalData不包含 的任何实例StringindexOf()因此永远不会返回索引。

If you want to access an instance of animalsby the name, you should use a Map<String,animals>.

如果要按animals名称访问 的实例,则应使用Map<String,animals>.

Map<String,animals> animalMap = new HashMap<String,animals>();
animalMap.put("Mat", new animals("Mat", "id");
animals animal = animalMap.get("Mat");

The proper use of indexOf()is to pass in an instance that is equal to an instance already in the collection. As others have pointed out, this will require you to define equals()to define what makes two instances equal. You should also override hashcode()when you override equals()because there is an assumed correlation.

的正确用法indexOf()是传入一个实例,该实例等于集合中已有的实例。正如其他人指出的那样,这将要求您equals()定义使两个实例相等的原因。您在覆盖hashcode()时也应该覆盖,equals()因为存在假定的相关性。

Note: The convention is to use CapitalCase for class names. Also, the class name should not be plural. You will have many instances of Animal, you may later make a class that is a collection of Aniamals, but that should not be the name of the main class.

注意:约定是使用大写字母作为类名。此外,类名不应该是复数。您将有许多 的实例Animal,稍后您可能会创建一个类,该类是Aniamals的集合,但这不应是主类的名称。

回答by aymeric

Here is the code I would use:

这是我将使用的代码:

public class animals {
    String Name, ID;
    static Map<String, animals> animalData = new HashMap<String, animals>();

    public animals(){}
    public animals(String name, String id){
        super();
        this.Name = name;
        this.ID = id;
    }

    public static void addAnimal(String name, String id){
        animalData.add(new animals(name, id));
    }

    // Returns null if no registered animal has this name.
    public animals search(String name){
        return this.animalData.get(name);
    }
}

This way, you make the searchmethod much faster (O(1)), you don't need to override the equalsmethod anymore.

这样,您可以使search方法更快(O(1)),您不再需要覆盖该equals方法。

Note that if animalDatais static, you should consider to make addAnimal()static as well since it's sort of a 'setter' for aniamalData.

请注意,如果animalData是静态的,您也应该考虑将其设为addAnimal()静态,因为它是aniamalData.

回答by ddyer

You need to define an "equals" method

您需要定义一个“equals”方法

回答by hilaia

You're looking for a String... You'd better use a HashMap I think...

你正在寻找一个字符串......我认为你最好使用一个HashMap......

But yeah you have to change your structure(which isn't very efficient)

但是,是的,您必须更改结构(效率不高)