Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java arraylist索引_Java_Arraylist_Indexing - Fatal编程技术网

Java arraylist索引

Java arraylist索引,java,arraylist,indexing,Java,Arraylist,Indexing,这是我的类动物。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;

这是我的类
动物。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);
    }
}
公共类动物{
字符串名称、ID;
静态ArrayList animalData=新ArrayList();
公共动物(){}
公共动物(字符串名称、字符串id){
超级();
this.Name=Name;
this.ID=ID;
}
public void addAnimal(字符串名称、字符串id){
添加(新动物(名称、id));
}
公共整数搜索(字符串名称){
返回此.animalData.indexOf(名称);
}
}
当我添加带有id的动物名称时,它正常工作,但当我使用搜索方法时,我只看到-1。也许我在这个类中尝试重写方法
equals
indexof
?帮我做这个


感谢您并为我的糟糕英语感到抱歉。

是的,在集合中使用对象并基于对象执行查找时,需要重写
equals()
hashcode()
方法


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

您需要定义一个“equals”方法,因为您正在查找字符串。。。 你最好用一个HashMap我想


但是你必须改变你的结构(这不是很有效)

你正在将
动物的实例添加到列表中。您正在按名称搜索实例。由于
animalData
不包含
String
的任何实例,
indexOf()
将永远不会返回索引

如果要通过名称访问
动物
的实例,应使用
映射

Map animalMap=newhashmap();
动物地图(Mat),新动物(Mat,id);
动物=动物映射获取(“Mat”);
indexOf()
的正确用法是传入一个等于集合中已有实例的实例。正如其他人所指出的,这将要求您定义
equals()
来定义使两个实例相等的因素。在重写
equals()时,还应重写
hashcode()
因为存在假定的相关性


注意:惯例是使用大写字母作为类名。此外,类名不应为复数。您将有许多
动物的实例,稍后您可以创建一个由
Aniamal
组成的类,但该类不应是主类的名称。

以下是我将使用的代码:

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);
    }
}
公共类动物{
字符串名称、ID;
静态映射animalData=newhashmap();
公共动物(){}
公共动物(字符串名称、字符串id){
超级();
this.Name=Name;
this.ID=ID;
}
公共静态void addAnimal(字符串名称、字符串id){
添加(新动物(名称、id));
}
//如果没有注册的动物具有此名称,则返回null。
公共动物搜索(字符串名称){
返回此.animalData.get(名称);
}
}
这样,您可以使
search
方法更快(O(1)),不再需要重写
equals
方法


<>请注意,如果<代码>动画数据< /代码>是静态的,则应该考虑使<代码> AddiMalId()/代码> static也是静态的,因为它是<代码> 'AiMaLaDATAs/COD> >的“设置器”。

是的,您需要重写等值方法。方法?@user1429570:查看此链接以了解更多信息如果您对equals/hashcode和使用eclipse有问题,请单击
Source->Generate hashcode()&equals()
Just having equals()可能不是有效的hashcode/equals覆盖契约。当我定义equals方法时,它的默认代码如何更改我不知道:您可能想要公共布尔equals(对象b){return(id.equals(b));}ty之类的东西,但我想了解这个问题;静态ArrayList animalData=new ArrayList();我可以添加新动物列表,例如:“猫”、“1”、“狗”、“2”。这正常工作,我显示所有项目,但当我想在动物数据列表中搜索“猫”时,我想获得“猫”的索引。(因为我需要索引,当我更改“猫”>“鸟”)动物数据。设置(索引,元素);我知道这个方法,但我需要“猫”索引。这是不可能的?@user1429570:你的意思是想访问“猫”吗根据您定义的
ID
或是否要通过列表中存在的实例的索引访问该实例?
indexOf()
将为您提供列表中实例的索引。它不会在列表中搜索名为“CAT”的实例,并将您分配的ID返回给您“1”;如果您想执行后面的操作,您应该将字符串映射到字符串,并使用name作为键,id作为值。哈希映射的效率如何降低?
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);
    }
}