Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Arraylist indexof problem

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Arraylist indexof problem

    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

    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);
        }
    }
    create method working normally and add object to list, but when i call animalIndex method i given this problem;
    **return only -1 why?**


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Arraylist indexof problem

    return only -1 why?
    What does the API doc say about the values that the indexOf() method returns? Read that for the explanation.

    What does the ArrayList: animals contain? You are looking in it for a String. It does not contain any String objects so indexOf() won't find the String: name.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. ArrayList problem
    By Renhik in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 8th, 2012, 11:48 AM
  2. .indexOf selective replacements
    By msinc210 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 30th, 2012, 04:33 PM
  3. newby Questions - indexOf help with an ArrayList
    By Bially in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 20th, 2011, 06:05 PM
  4. Problem with ArrayList
    By waltersk20 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 26th, 2010, 08:37 PM
  5. contains vs. indexOf
    By mgrootsch in forum Java Theory & Questions
    Replies: 5
    Last Post: September 17th, 2009, 08:09 AM