共計(jì) 342 個(gè)字符,預(yù)計(jì)需要花費(fèi) 1 分鐘才能閱讀完成。
在 Java 中,靜態(tài) Map 可以通過靜態(tài)代碼塊或靜態(tài)方法進(jìn)行初始化。
- 使用靜態(tài)代碼塊:
public class MyClass {
private static Map map;
static {map = new HashMap();
map.put(1, "One");
map.put(2, "Two");
map.put(3, "Three");
}
}
- 使用靜態(tài)方法:
public class MyClass {private static Map map = initMap();
private static Map initMap() {Map map = new HashMap();
map.put(1, "One");
map.put(2, "Two");
map.put(3, "Three");
return map;
}
}
丸趣 TV 網(wǎng) – 提供最優(yōu)質(zhì)的資源集合!
正文完