如何使用Apex增加地图中的值?

Zat*_*ath 4 salesforce map increment visualforce apex-code

有没有办法在不需要第二个变量的情况下增加地图中的值?

例如,这不起作用:

counts = new Map<string,integer>();
counts.put('month_total',0);
counts.put('month_total',counts.get['month_total']++);
Run Code Online (Sandbox Code Playgroud)

它返回"字段表达式的初始术语必须是具体的SObject:MAP"

相反,我需要这样做:

counts = new Map<string,integer>();
counts.put('month_total',0);
integer temp = 0;
temp++;
counts.put('month_total',temp);
Run Code Online (Sandbox Code Playgroud)

有没有办法增加而不需要额外的变量?

Jer*_*oss 5

更换

counts.put('month_total',counts.get['month_total']++);
Run Code Online (Sandbox Code Playgroud)

counts.put('month_total',counts.get('month_total')++);
Run Code Online (Sandbox Code Playgroud)

  • 非常感谢,解决了它。但我确实需要将其从“++”更改为“+1”,因为我收到“无法分配表达式”错误。也许在你的答案中更新一下? (3认同)