0

I'm stuck on this problem i make a class Person in which it has only two properties name and age. I'm taking details of that person as a user input and saving as a object in ArrayList but i want to move that ArrayList object properties that user enter from keyboard into separate arrays like all names of person into String array and all ages of person into int array from that ArrayList. Is there a way to do that?
Code:

public class Person {

    String name;
    int age;

}

public class Main {

    public static void main(String[] args) {


        Scanner s = new Scanner(System.in);
        ArrayList<Person> arr = new ArrayList<Person>();
        Person person = null;



        for(int i=0;i<2;i++){
            person = new Person();
            System.out.println("Enter Person "+(i+1)+" Record");
            int age = 0;
            System.out.print("Enter Name : ");
            String str = s.nextLine();
            person.name = str;
            System.out.print("Enter Age : ");
            age = s.nextInt();
            s.nextLine();
            person.age = age;
            arr.add(person);
            person = null;
            System.out.println("Record "+(i+1)+" Successful Fill");

        }
            String[] stringarray = new String[arr.size()];
            int[] intarray = new int[arr.size()];

    }

}
hamel123
  • 304
  • 4
  • 18

3 Answers3

0

There are a lot of ways to do this. One would be to create ArrayLists instead for ages and names and add to them each time new values are pulled in.

ArrayList<String> names = new ArrayList<>();
ArrayList<int> ages = new ArrayList<>();

for(int i = 0; i < 2; i++){
    // create person object
    names.add(str);
    ages.add(age);
}

And if they absolutely NEED to be Arrays then you can convert with:

String[] nameArray = names.toArray(new String[0]);
String[] ageArray = ages.toArray(new int[0]);
Susannah Potts
  • 837
  • 1
  • 12
  • 32
0

I don't know any fancy ways to go about doing this, but a way to do it, whenever you need them, is make a method to loop through all Person objects and make two arrays of the information.

So...

int[] ages = new int[arr.size()];
String[] names = new String[arr.size()];
for(int i = 0; i < arr.size(); i++){
     //assign attributes
}

Then do whatever you want with these arrays...

Again, there may be a better way to do it, but this will definitel work, although it is not too efficient.


However, in your for loop, right where you add the Person objects to the array, you can just add them to a declared arraylist in the beginning.

Then if you truly need it to be a "List" as opposed to "ArrayList", you can convert the arraylist to a list. Here is a good example of that.

Community
  • 1
  • 1
Richard
  • 138
  • 1
  • 1
  • 9
0

If you want to hold your arraylist with person-objects and fill the arrays after all values read in create a new function:

private void add2Arrays(List<Person> list) {
  for(int i = 0; i < list.size(); i++) {
    Person p = list.get(i);
    stringarray[i] = p.getName();
    intarray[i] = p.getAge();
  }
}
beeb
  • 1,615
  • 1
  • 12
  • 24