发现一个 ArrayList 很奇怪的现象 一个 ArrayList X 和一个 ArrayList> Y 想要实现: Y = [X, X] ArrayList X = new ArrayList(); ArrayList> Y = new ArrayList>(); X.add(Double.valueOf(0)); // 先set X X.add(Double.valueOf(1)); Y.add(X); // 把 X 加入 Y System.out.println(Y); X.set(0,Double.valueOf(0.5)); // 然后修改 X : X.set(1,Double.valueOf(1.5)); Y.add(X); // 再把 X 加入 Y System.out.println(Y); 应该是第一次打印的 Y 是 : [[0, 1]] 第二次打印的 Y 是 : [[0, 1], [0.5, 1.
s*e
4 楼
需要一份FO Licht's World Ethanol & Biofuels Report。不知道有没有人能帮个忙… …或者任何其他能找到ethanol的世界各国生产数据的地方,谢谢了……
s*y
5 楼
忍耐,或者满心欢喜的去接受
【在 w*******n 的大作中提到】 : LD说我家的两个小子最近有点累人啊。。。
P*e
6 楼
That's exactly, why I said, clone method should be on the textbook. It's pass by value, reference in Java, you did not really create an new object and add to the Y, you added the reference to the Y. Do this: Y.add(X.clone()); //physically clone a copy of X and add it to Y, rather than reference. each time. You'll get what you want.
【在 b*********n 的大作中提到】 : 发现一个 ArrayList 很奇怪的现象 : 一个 ArrayList X : 和一个 ArrayList> Y : 想要实现: Y = [X, X] : ArrayList X = new ArrayList(); : ArrayList> Y = new ArrayList>(); : X.add(Double.valueOf(0)); // 先set X : X.add(Double.valueOf(1)); : Y.add(X); // 把 X 加入 Y : System.out.println(Y);
wh
7 楼
以前医院给我一页纸,说要一个一个原因去找:是不是饿了?困了?要换尿布了?病了 ?想出去社交一下、出门找朋友玩?白天多带小孩出门转转,他们会觉得新鲜,晚上一 般会睡得好。另外小孩晚上睡觉之前一般会fussy一点,说是letting off steam,要把 一天的精力都发泄完毕才酣然入睡。这时候就让他们哭一阵,哭累了就睡了。 不过我的理论是小孩哭闹是没有道理的,就像有的小孩能睡,有的不爱睡,也是没有道 理的,天生的。摊上能睡的小孩是你福气;摊上不爱睡的算你倒霉。
【在 w*******n 的大作中提到】 : LD说我家的两个小子最近有点累人啊。。。
b*n
8 楼
wow! 这下出来了 太谢谢了!
【在 P********e 的大作中提到】 : That's exactly, why I said, clone method should be on the textbook. : It's pass by value, reference in Java, you did not really create an new : object and add to the Y, you added the reference to the Y. : Do this: : Y.add(X.clone()); //physically clone a copy of X and add it to Y, rather : than reference. : each time. : You'll get what you want.