mainData.java
public class mainData{ public animalsData animal = new animalsData(); public static void main(String[] args) { mainData mn = new mainData(); mn.newAnimal(); } public void newAnimal(){ animal.create("CAT", "4"); animal.create("DOG", "5"); System.out.println(animal.animalIndex("DOG")); } }
animalsData.java
create method working normally and add object to list, but when i call animalIndex method i given this problem;public class animalsData{ String name, l; ArrayList<animalsData> animals = new ArrayList<animalsData>(); public animalsData(){}//def constructor public animalsData(String name, String l){ super(); this.name= name; this.l= l; } public void create(String name, String l){ animals.add(new animalsData(name, l)); } public int animalIndex(String name){ return this.animals.indexOf(name); } }
**return only -1 why?**