[Java] + copy list object does not affect the original value

 

 1     @Test
 2     public void testeee() {
 3         List<DeliveryContentVO> contentVOList = new ArrayList<>();
 4         DeliveryContentVO contentVO1 = new DeliveryContentVO();
 5         contentVO1.setId("1");
 6         contentVO1.setIndex(1);
 7         contentVOList.add(contentVO1);
 8 
 9         DeliveryContentVO contentVO2 = new DeliveryContentVO();
10         try {
11             BeanUtils.copyProperties(contentVO2,contentVO1);
12         } catch (Exception e) {
13             e.printStackTrace();
14         }
15         contentVO2.setId("2");
16         contentVO2.setIndex(2);
17         contentVOList.add(contentVO2);
18 
19         System.out.println(JSONObject.toJSON(contentVOList));
20     }
View Code

result:

 

 

Guess you like

Origin www.cnblogs.com/danhuai/p/11883325.html