lla*_*ato 1 java foreach key-value
我正在尝试将PHP脚本转换为Java脚本,但在foreach循环中遇到了一些问题.在PHP脚本中,我有一个foreach,它接受key:value对,并以此为基础执行str_replace.
foreach ($pValues AS $vKey => $vValue)
$vString = str_replace("{".$vKey."}", "'".$vValue."'", $vString);
Run Code Online (Sandbox Code Playgroud)
我已经尝试过复制这个Java而没有成功,我需要从数组中获取密钥以在字符串替换函数中使用但不能在我的生活中找出它可能从哪里获得密钥名称传入的数组.
这是正确的方式,还是我完全离开了??我应该使用这种ImmutablePair方法吗?
for (String vKey : pValues)
// String replace
Run Code Online (Sandbox Code Playgroud)
这里希望有一个简单的方法来获得Java中的密钥:值对,提前感谢.
这可以通过使用Map作为数据结构然后使用entryset迭代它来实现.
Map<K,V> entries= new HashMap<>();
for(Entry<K,V> entry : entries.entrySet()){
// you can get key by entry.getKey() and value by entry.getValue()
// or set new value by entry.setValue(V value)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15300 次 |
| 最近记录: |