Basically I have the following code:
List<Object[]> tes = new ArrayList<>();
Object[] ar1 = {"John","Cena","2347867686"};
Object[] ar2 = {"Peter","Smith","978787878"};
tes.add(ar1);
tes.add(ar2);
I have the following PersonDto class as follows:
public class PersonDto {
String name;
String surname;
String phone;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
That's what I was trying:
for (Object[] object : tes) {
TestDto testDto = new TestDto();
for (Object object2 : object) {
System.out.println(object2);
}
System.out.println("second");
}
But i am not able to know when to set surname, name or phone in the for loop. So basically I am trying to map each array to a PersonDto from the list as follows:
So the expected result would be:
- The first person(PersonDto) would have name set as John, surname set as Cena and phone set as 2347867686.
- The second person(PersonDto) would have name set as Peter, surname set as Smith and phone set as 978787878.
There can be many arrays in the list and each of them will always have three attributes (name, surname, phone).
name,surname, andphoneas parameters and sets the values inthisto the associated values, or create the object and then call their setters (but the first way is preferred)