Java 特殊性领会

时间:2023-03-28 12:29:26

1. 字符串比较绝对不能用 == 而必须是 xx.equals()

2. 多有对象new 后都是以引用的方式存在着

3. 数组list, map 类型都不能边用for 循环边删除,迭代器可以

eg:

Iterator<Map.Entry<String, GoodsItem>> iterator = mGoodsPool.entrySet().iterator();
while(iterator.hasNext()) {
Map.Entry<String, GoodsItem> entry = iterator.next();
if(entry.getValue().getCateId().equals(categoryId)) {
entry.getValue().setReferCut();
if(entry.getValue().getReferCount() == 0) {
iterator.remove();
}
}
}
for(int i = 0; i < list.size(); i++) {
if(mGoodsPool.containsKey(list.get(i).getItemId())) {
this.mGoodsPool.get(list.get(i).getItemId()).setReferAdd();
list.set(i, mGoodsPool.get(list.get(i).getItemId()));
} else {
this.mGoodsPool.put(list.get(i).getItemId(), list.get(i));
this.mGoodsPool.get(list.get(i).getItemId()).setReferAdd();
}