3

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..

1
  • Yes, you need to override the equals method. Commented Sep 2, 2012 at 18:36

5 Answers 5

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

ty but i want to learn this question; static ArrayList<animals> animalData = new ArrayList<animals>(); i can add the list new animals for example: "CAT", "1"; "DOG", "2" .. this work normally and i show all item but; when i want to search "CAT" in animalData list i want to get "CAT"'s index.( Because i need index, when i change "CAT" > "BIRD") animalData .set(index, element); i know this method but i need "CAT" index. it's impossible?
@user1429570: Do you mean you want to access "CAT" based on the ID you define? Or do you want to access it by the index the instance exists in the list? indexOf() will give you the index of the instance in the list. It will not search the list for the instance with the name "CAT" and return to you the id you assigned of "1"; If you want to do the later, you should make a map of string to string and use name as the key and id as the value.
4

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.

3 Comments

What do you mean by it doesn't work? Can you update question with your equals() and hashcode() methods?
@user1429570: See this link for more information technofundo.com/tech/java/equalhash.html
If you have problem with equals/hashcode AND using eclipse, click Source -> Generate hashCode() & equals()
0

You need to define an "equals" method

3 Comments

Just having equlas() may not be valid hashcode/equals override contract.
when i define equals method its default code how i change i dont know :s
you probably want something like public boolean equals(Object b) { return ( id.equals(b)); }
0

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)

1 Comment

How is a HashMap any less efficent?
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.