2

I have a simple code storing multiple objects into ArrayList through 'for' iterator.

@RequestMapping("dataInsert")
public String dataInsert(String pageNum, Model model) {

    Supplier supplier = new Supplier();
    List<Supplier> list = new ArrayList<Supplier>();
    int number = ss.maxNum(supplier);
    for (int i = 0; i < 5; i++) {
        supplier.setNum(number + i);
        supplier.setName("supplier" + i);
        supplier.setDel("n");
        supplier.setAddr("address" + i);

        list.add(supplier);

        System.out.println(i + " : " + supplier);   
    }

    System.out.println(list);
    int result = ss.insertList(list, supplier);

    model.addAttribute("result", result);
    model.addAttribute("pageNum", pageNum);
    return "supplier/Insert";
}

Console output below shows the iterated number of objects:

0 : Supplier [num=13, name=supplier0, tel=null, addr=address0, del=n]
1 : Supplier [num=14, name=supplier1, tel=null, addr=address1, del=n]
2 : Supplier [num=15, name=supplier2, tel=null, addr=address2, del=n]
3 : Supplier [num=16, name=supplier3, tel=null, addr=address3, del=n]
4 : Supplier [num=17, name=supplier4, tel=null, addr=address4, del=n]

The console output come up from:

System.out.println(list);

Outside of for statement which represents the List shows the same content for each object.

[Supplier [num=17, name=supplier4, tel=null, addr=address4, del=n],
Supplier [num=17, name=supplier4, tel=null, addr=address4, del=n],
Supplier [num=17, name=supplier4, tel=null, addr=address4, del=n],
Supplier [num=17, name=supplier4, tel=null, addr=address4, del=n],
Supplier [num=17, name=supplier4, tel=null, addr=address4, del=n]
]

What I want to do is to create a list that has objects having different values. I guess something is wrong on the code. I thought the add() method in ArrayList is appending parameter value to existing list object. Does anybody know why this happens?

Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
Connor Lee
  • 69
  • 6

2 Answers2

2

You only store one Supplier object in your List (you add the same object 5 times), and you keep modifying it in your loop.

You should create a new instance for each iteration of your loop:

List<Supplier> list = new ArrayList<Supplier>();
int number = ss.maxNum(supplier);
for (int i = 0; i < 5; i++) {
    Supplier supplier = new Supplier();
    supplier.setNum(number + i);
    supplier.setName("supplier" + i);
    supplier.setDel("n");
    supplier.setAddr("address" + i);

    list.add(supplier);
    System.out.println(i + " : " + supplier);
}
Eran
  • 387,369
  • 54
  • 702
  • 768
1

You add to the List the same Supplier over again and again. This results that the Supplier setters are called repeatedly and amend each Supplier in the List. So every Supplier is the same as the last one.

The key is to create a new Supplier with each iteration and add it to the List:

List<Supplier> list = new ArrayList<Supplier>();
int number = ss.maxNum(supplier);
for (int i = 0; i < 5; i++) {
    Supplier supplier = new Supplier();
    // Supplier setters
    list.add(supplier);
}
Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183