I trying to understand the ArrayList

let say the user input in

1st Person
Name : Alex
Sex : M
Dog Name: Java

2 Person
Name: Joe
Sex: M
Dog Name: Is

3 Person

Name: Anna
Sex : F
Dog Name: Fun

in Arraylist how can you separate out from each person.

let say i only want to deal with Anna and her dog. how can i do it ? In other word, how can i know which dog belong to which owner. I dont want to display out like Alex dog name is Fun that is wrong!

Other thing is good to create an arraylist (if not, can you show an example code?)with asking user how many dog they have? and the name go long with it?

Class Person
{
    string name;
    string sex;
    string Dog_name;
}
.....
ArrayList<Person> persons = new  ArrayList<Person>();

Now if u iterate through this. you can get the object, it will ensure u wont get a wrong dog's name.

The second part of your question can also be done using an arraylist. Only your class structure will change to something like this:

Class Person
{
    string name;
    string sex;
    ArrayList<string> Dog_names;
}

Obvoisly arraylist has its pros and cons, when to use a arraylist and when not to depends on your requirements i reckon.

just a small remark on that. Starting from java 7,

ArrayList<Person> persons = new ArrayList<Person>();

can be replaced by:

List<Person> person = new ArrayList<>();

also: don't forget String has a capital S.

in your point to use a List to check the different dogs of an owner, I would recommend creating a List<Dog> (you'll have to create that type, though) instead of a List<String>.

if you have a list of Strings as dogs, and you have "Pluto Bluto" as dog, for a List<String> this might be both one dog, with name "Pluto Bluto", or two dogs, with names "Pluto" and "Bluto", so it's easier to "confuse input".

Class Person

package daniweb;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author JoZulyk http://inovawebstudio.com
 */
public class Person {
    private String name = "";
    private String sex = "";
    private List<Dog> dogs = new ArrayList<>();


    public Person(){
    }
    /**
     * @return the dogs
     */
    public List<Dog> getDogs() {
        return dogs;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @return the sex
     */
    public String getSex() {
        return sex;
    }

    /**
     * @param dogs the dogs to set
     */
    public void setDogs(List<Dog> dogs) {
        this.dogs = dogs;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @param sex the sex to set
     */
    public void setSex(String sex) {
        this.sex = sex;
    }


}

Class Dog (Here is important Override the method toString - needed for print in an list):

package daniweb;

/**
 *
 * @author JoZulyk http://inovawebstudio.com
 */
public class Dog {
    private String name;

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString(){
        return name;
    }

}

Class Daniweb:

package daniweb;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author JoZulyk
 */
public class Daniweb {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        //u make your own code for input
        Person first = new Person();
        first.setName("Alex");
        first.setSex("M");
        Dog df1 = new Dog();
        df1.setName("Java");
        first.getDogs().add(df1);
        //second person
        Person sec = new Person();
        sec.setName("Joe");
        sec.setSex("M");
        Dog ds1 = new Dog();
        ds1.setName("Is");
        sec.getDogs().add(ds1);
        //third person
        Person third = new Person();
        third.setName("Anna");
        third.setSex("F");
        Dog dt1 = new Dog();
        dt1.setName("Fun");
        third.getDogs().add(dt1);
        //List all people
        List<Person> persons = new ArrayList<>();
        persons.add(first);
        persons.add(sec);
        persons.add(third);
        //then if u are find any ... make it
        Iterator<Person> it = persons.iterator();
        Person found = new Person();
        while(it.hasNext()){
            Person p = (Person)it.next();
            if(p.getName().equals("Anna")){
                found = p;
                break;
            }else{
                found = null;
            }
        }
        //printing the result
        if(found!=null){
            System.out.println("The dog of "+found.getName() + " is(are): "+found.getDogs().toString());
        }
    }

}

The output are (when Anna have 1 dog):

The dog of Anna is(are): [Fun]

If u need 2 or more Dogs ... only need add it at the Person. See the code bellow:

package daniweb;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author JoZulyk
 */
public class Daniweb {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        //u make your own code for input
        Person first = new Person();
        first.setName("Alex");
        first.setSex("M");
        Dog df1 = new Dog();
        df1.setName("Java");
        first.getDogs().add(df1);
        //second person
        Person sec = new Person();
        sec.setName("Joe");
        sec.setSex("M");
        Dog ds1 = new Dog();
        ds1.setName("Is");
        sec.getDogs().add(ds1);
        //third person
        Person third = new Person();
        third.setName("Anna");
        third.setSex("F");
        Dog dt1 = new Dog();
        dt1.setName("Fun");
        //ADD NEW DOG (FOR 2 OR MORE) IMPORTANT!!!
        Dog dt2 = new Dog();
        dt2.setName("Thunder");
        Dog dt3 = new Dog();
        dt3.setName("Mr. Wonderfull");
        third.getDogs().add(dt1);
        third.getDogs().add(dt2);
        third.getDogs().add(dt3);
        //END OF ADDING MORE DOGS IMPORTANT!!!
        //List all people
        List<Person> persons = new ArrayList<>();
        persons.add(first);
        persons.add(sec);
        persons.add(third);
        //then if u are find any ... make it
        Iterator<Person> it = persons.iterator();
        Person found = new Person();
        while(it.hasNext()){
            Person p = (Person)it.next();
            if(p.getName().equals("Anna")){
                found = p;
                break;
            }else{
                found = null;
            }
        }
        //printing the result
        if(found!=null){
            System.out.println("The dog of "+found.getName() + " is(are): "+found.getDogs().toString());
        }
    }

}

The output is:

The dog of Anna is(are): [Fun, Thunder, Mr. Wonderfull]

NOW please make how SOLVED! ;) have a great Day!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.