2

I'm new to java and I want to get all data from a class.

This is how I add data to the class:

String[] arrNames = { "Andrew", "James" ... };
...

for(int i = 0; i < arrNames.length; i++){
   Person person = new Person();
   person.setId(i + 1);
   person.setName(arrNames[i]);
   person.setLastname(arrLastnames[i]);
   person.setIdentifier(arrIds[i]);
   person.setAddress(arrAddreses[i]);
}

How can I get all those data that I have added to the class?

for(Person p : ???){
   System.out.println(p.getName());
}
0

6 Answers 6

14

You need to create a List<Person> and add the each person in the loop to that list.

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

for(int i = 0; i < arrNames.length; i++){
   // Create Person
   // Set Attributes
   personList.add(person);
}

And then iterate over that list to get each Person instance back: -

for (Person person: personList) {
    System.out.println(person.getName());
}
Sign up to request clarification or add additional context in comments.

Comments

10
List<Person> list = ArrayList<Person>();

for(int i = 0; i < arrNames.length; i++){
   Person person = new Person();
   person.setId(i + 1);
   person.setName(arrNames[i]);
   person.setLastname(arrLastnames[i]);
   person.setIdentifier(arrIds[i]);
   person.setAddress(arrAddreses[i]);

   list.add(person); // adding each person object to the list.
}

Comments

3

So quick ArrayList primer:

List<Person> list = new ArrayList<Person>();
//Code to add stuff

for (Person p : list) {
  //Do something with p to your heart's desire.
}

Or

for (int i = 0; i < list.size(); i++) {
  Person reference = list.get(i);
}

To add stuff however your loop is flawed. Each iteration you just overwrite person with the new data. You need to add it to the list.

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

for(int i = 0; i < arrNames.length; i++){
   Person person = new Person();
   person.setId(i + 1);
   person.setName(arrNames[i]);
   person.setLastname(arrLastnames[i]);
   person.setIdentifier(arrIds[i]);
   person.setAddress(arrAddreses[i]);

   people.add(person);
}

Comments

1

Put Person inside list for example:

List<Person> persons = new ArrayList<Person>();
for(int i = 0; i < arrNames.length; i++){
   Person person = new Person();
   person.setId(i + 1);
   person.setName(arrNames[i]);
   person.setLastname(arrLastnames[i]);
   person.setIdentifier(arrIds[i]);
   person.setAddress(arrAddreses[i]);
   persons.add(person);
}
for(Person p : persons){
   System.out.println(p.getName());
}

Comments

1

Introduce a list with persons and add the objects to it in your loop

String[] arrNames = { "Andrew", "James" ... };
...

List<Person> persons = new ArrayList<Person();    
for(int i = 0; i < arrNames.length; i++){
   Person person = new Person();
   person.setId(i + 1);
   person.setName(arrNames[i]);
   person.setLastname(arrLastnames[i]);
   person.setIdentifier(arrIds[i]);
   person.setAddress(arrAddreses[i]);

   persons.add(person);
}

Then you can iterate over the list like this

for(Person p : persons){
   System.out.println(p.getName());
}

Comments

1
List<Person> people = Arrays.asList(new Person(1, "Andrew", "Surname1", "address1"), new Person(2, "James", "Surname2", "address2"));

for(Person p : people){
   System.out.println(p.getName());
}

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.