【发布时间】:2020-04-17 05:17:23
【问题描述】:
我尝试如下。 我想将一个 jsonobject 的数组列表复制到另一个而不引用相同的...., 谁能解释一下我做错了什么??
ArrayList<JSONObject> a = new ArrayList<JSONObject>();
JSONObject aa = new JSONObject();
aa.put("key1", "old");
aa.put("key2", "old");
aa.put("key3", "old");
a.add(aa); `T`
ArrayList<JSONObject> b = new ArrayList<JSONObject>();
b = a;
b.get(0).put("key1", "new");
System.out.println(a);
System.out.println(b);
【问题讨论】:
-
List<JSONObject> b = a.stream().collect(Collectors.toList());Java-8 一行就可以搞定。 -
这也会影响两个列表。,
-
嘿 Sambath,我的回答解决了问题吗?