jeudi 13 mai 2021

Java: Set Different Object Fields Based On Array Size

I have an object that has the following fields:

@Data
public class Person {
    private String code1;
    private String code2;
    private String code3;
    private String code4;
};

Then I need to set those code fields based on an input array size:

 if array has size 1, set code1
 if array has size 2, set code1, code2
 if array has size 3, set code1, code2, code3
 if array has size 4, set code1, code2, code3, code4

I did it using 4 if blocks. Is there a way to do it in a loop so that when there are more code fields to add, I don't need to keep adding the if block? Thanks.





Aucun commentaire:

Enregistrer un commentaire