Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

261
Views
Recuperar array[] de valores de ArrayList de objetos

Digamos que tengo una ArrayList de objetos. Por ejemplo: ArrayList<Person> personList , donde cada Person tiene 2 variables de clase String name e int age . Cada una de estas variables tiene sus propios métodos captadores getName() y getAge() .

¿Cuál es la forma más sencilla de recuperar una matriz (o ArrayList) de int ages[] ?

Tenga en cuenta que esta pregunta es similar a la detalladamente titulada " Recuperar una matriz de valores asignados a un miembro de clase en particular de una matriz de objetos en Java ", aunque sin la restricción arbitraria de bucles for, y usando una ArrayList en lugar de una Array.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Cree una matriz de destino del mismo tamaño que la lista, luego repita la lista y agregue la age de cada elemento a la matriz de destino.

over 4 years ago · Santiago Trujillo Report

0

Numerosas formas de hacer esto, aquí hay una.

Primero coloque las edades en una lista (usando un flujo java8) y luego convierta la lista en una matriz.

 public int[] getAges() { return personList.stream() .mapToInt(Person::getAge) .toArray(); }
over 4 years ago · Santiago Trujillo Report

0

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; }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!