vendredi 16 juillet 2021

How to reduce the number of times IF-else If in JAVA

Acquire data from the database. Stores the collected data in a record called NAME in the first list. It can contain up to 30 cases of data. I want to save the record NAME to the second list of records. The second record is from NAME1 to 30. I'd like to contain the data in the second record in order.

First List

    List<NAME> nameList = new ArrayList<NAME>();
    NAME name = new NAME();
    name.setName("A");
    nameList.add(name);
    
    name = new NAME();
    name.setName("B");
    nameList.add(name);
    .....

    int count = 1;
    for (NAME getname : nameList) {
        RENAME rename = new RENAME();
        if (count == 1) {
            rename.setName1(getname.getName());
        } else if (count == 2) {
            rename.setName2(getname.getName());
        }
        ....
        count++;
    }

Aucun commentaire:

Enregistrer un commentaire