LoginSignup
2
3

More than 5 years have passed since last update.

【Salesforce初心者】Apex開発でハマった(already output)事件

Last updated at Posted at 2017-05-17

ハマりましたね。。初級のトラップ笑
already outputというDEBUGメッセージに。

コード①

Map<Integer, List<String>> mapA = new Map<Integer, List<String>>();
List<String> liststr = new List<String>();

liststr.add('a');
liststr.add('b');

//System.debug(liststr);

mapA.put(1, liststr);

liststr.clear();

liststr.add('c');
liststr.add('d');

mapA.put(2, liststr);

System.debug(liststr);
System.debug(mapA);

コード②

Map<Integer, List<String>> mapA = new Map<Integer, List<String>>();
List<String> liststr = new List<String>();

for (Integer j =0; j<2; j++){
liststr.clear();
for (Integer i = 0; i<2; i++){
liststr.add('a'+i);
}
mapA.put(j, liststr);
}
//System.debug(liststr);
System.debug(liststr);
System.debug(mapA);

上記の二つのコード(実際同じだけど)、をコンソール上で実行したら、

DEBUG|(c, d)
DEBUG|{1=(c, d), 2=(already output)}

'a'と'b'がなぜ入らないのかなと結構悩んでました笑
毎回clearしているし。

で、理由は
MapAにputしているのは、Listの中の値ではなくて、
そう!参照しているアドレスだから!
以上。笑

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3