3

Let's say I have an ArrayList of objects. For example: ArrayList<Person> personList, where each Person has 2 class variables String name and int age. These variables each have their own getter methods getName() and getAge().

What is the simplest way to retrieve an array (or ArrayList) of int ages[]?

Note this question is similar to the verbosely titled "Retrieve an array of values assigned to a particular class member from an array of objects in java", though without the arbitrary restriction on for-loops, and using an ArrayList instead of an Array.

4
  • Why doesn't the linked question solve your problem? Commented Apr 28, 2017 at 21:01
  • The linked question had an arbitrary restrictions on using for-loops, and was retrieving from an an Array[] of objects, than using an ArrayList Commented Apr 28, 2017 at 21:02
  • Possible duplicate of Retrieve an array of values assigned to a particular class member from an array of objects in java Commented Apr 28, 2017 at 21:02
  • 1
    It's functionally identical though Commented Apr 28, 2017 at 21:03

3 Answers 3

5

Create a target array of the same size as the list then iterate through the list and add each element's age to the target array.

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

2 Comments

This worked perfectly! I used the following implementation. int ages[] = new int[personList.size()]; for (int i = 0; i < ages.length; i++) { ages[i] = personList.get(i).getAge(); }
@StevenVascellaro, I have done a similar implementation below :p
4

Numerous ways to do this -- here is one.

First get the ages into a list (using a java8 stream), and then convert the list into an array.

public int[] getAges() {
    return personList.stream()
        .mapToInt(Person::getAge)
        .toArray();
}

3 Comments

Hint, use mapToInt, you can call toArray on the stream and get an int[] directly.
@JornVernee ah nice tip! Please feel free to edit my answer with your improvement.
Is there any way to use streams with a function like Agent.getResource("angle"), or would this only work for methods without arguments?
2
Person P1 = new Person("Dev", 25);
Person P2 = new Person("Andy", 12);
Person P3 = new Person("Mark", 20);
Person P4 = new Person("Jon", 33);

ArrayList<Person> personList = new ArrayList<>(Arrays.asList(new Person[] { P1, P2, P3, P4 }));
int[] ages = getPersonAges(personList); // [25, 12, 20, 33]

private static int[] getPersonAges(ArrayList<Person> personList) {
    int[] ages = new int[personList.size()];
    int idx = 0;

    for (Person P : personList) {    // Iterate through the personList
        int age = P.getAge();
        ages[idx++] = age;
    }

    return ages;
}

6 Comments

Is there a reason not to one-line the for loop to ages[idx++] = P.getAge()?
@StevenVascellaro No. Those two code segments would be identical. It is acceptable to do either.
I have written on two lines just to explain that the return type from P.getAge() is of type int or autoboxed to int.
@Steven, remember you can always mark the answer accepted that you found simple and helpful. Thanks :D
@DevendraLattu Thanks. Typically, I try to avoid rushing to accept an answer so I can avoid the Fastest Gun in the West problem.
|

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.