简体   繁体   中英

Java - How to iterate through an ArrayList for adding elements?

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());
}

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());
}
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.
}

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);
}

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());
}

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());
}
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());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM