I have created a class named Test with (name and surname variables) plus getter and setter, then I made a List with Test type. I use set method to set variables value and then I try to add klass.setName(nminput)
and klass.setSurname(surnminput);
to the List but only one value being added even with loops. Is there anyway to add item to a List from class variable?
Scanner input = new Scanner(System.in);
Test klass = new Test("name", "surname");
List < Test > list = new ArrayList < > ();
for (int i = 0; i < 3; i++) {
String nminput = input.next();
String surnminput = input.next();
klass.setName(nminput);
klass.setSurname(surnminput);
list.add(klass);
i++;
}
System.out.println(list);