kibar
kibar

Reputation: 824

Java arraylist indexof

Here is my class 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 equals or indexof in this class? help me for this

Thank you and sorry for my bad english..

Upvotes: 3

Views: 10368

Answers (5)

unholysampler
unholysampler

Reputation: 17321

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

If you want to access an instance of animals by the name, you should use a 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.

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.

Upvotes: 4

aymeric
aymeric

Reputation: 3895

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 search method much faster (O(1)), you don't need to override the equals method anymore.

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

Upvotes: 0

hilaia
hilaia

Reputation: 1

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

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

Upvotes: 0

ddyer
ddyer

Reputation: 1788

You need to define an "equals" method

Upvotes: 0

kosa
kosa

Reputation: 66637

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

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.

Upvotes: 4

Related Questions