2

I have an arraylist of class Person

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

Inside that Person class i have some variables associated with that person.

public class Person {
    String Name;
    String Address;
    String Phonenumber;
}

Now is there a way using the people arraylist to get an arraylist of Name?

2
  • Why do you need a separate array list? What's wrong with (people.get(index)).getName()? Commented Dec 7, 2013 at 4:19
  • Epickel, my answer may provide a convenient way of keeping the two arrays in sync if you must have both arrays. Commented Dec 7, 2013 at 4:36

3 Answers 3

3

You have to iterate over it.

List<String> names = new ArrayList<>();

for(Person person : people) {
   names.add(person.getName()); // Assuming you have a getter
}
Sign up to request clarification or add additional context in comments.

9 Comments

but will the names arraylist update if I add a new person to the people arraylist?
No, definitely not. You will have to extend ArrayList and provide events or just call it manually when changing the people list.
@Epickel: that's incorrect, Name isn't public. It has no modifier. Just to be safe I have commented that I assume a getter, as you can see.
I'm just wondering if this is even the correct approach as a way to store people for creating a contact application because it seems like such a hassle.
Why do you need a separate array? Just access the getName() method on each index of the array of people.
|
0

Now is there a way using the people arraylist to get an arraylist of Name?

Yes. Define a name arraylist as ArrayList<String>. Than, iterate over ArraList<Person> and put value on name arraylist.

List<String> nameList = new ArrayList<>();

for(Person person : people) {
   nameList.add(person.getName());
}

Comments

0

If you really want to maintain a pair of ArrayLists, one containing Person objects, and the other containing the Name property of the person objects, you could always create another class:

public class PersonList {
    private ArrayList<Person> people;
    private ArrayList<String> names;

    public PersonList() {
        people = new ArrayList<>();
        names = new ArrayList<>();
    }

    public PersonList(ArrayList<Person> p) {
        people = p;
        names = new ArrayList<>();
        for(Person person : p) {
            names.add(p.getName());
        }
    }

    public ArrayList<Person> getPeople() {
        return people;
    }

    public ArrayList<String> getNames() {
        return names;
    }

    public void add(Person p) {
        people.add(p);
        names.add(p.getName());
    }

    public Person getPerson(int index) {
        return people.get(index);
    }

    public String getName(int index) {
        return names.get(index);
    }

    // more methods to continue to replicate the properties of ArrayList...
    // just depends what you need
}

You will have to continue adding methods to this class so that this class can do everything that you can do on a single ArrayList. And really, this class is just a convenient way of making it easier to work maintain two different ArrayLists at the same time.

Anyway, now in your main code, you can instantiate an object of PersonList instead of an array list:

PersonList people = new PersonList();

And add to people. Or you could even just use a regular array list up until you need the names list, and instantiate a PersonList with the other constructor I provided:

// Assuming ArrayList<People> arrPeople exists...
PersonList people = new PersonList(arrPeople);

And now the people object will contain an ArrayList identical to arrPeople, as well as a matching names list containing all the names. And no matter how you instantiate a PersonList, calling the add method on a PersonList (same syntax as an ArrayList if you set this class up correctly), and it will keep both array lists that the class manages in sync.

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.