数组
普通数组
声明:
int [] intArray0;
intArray0 = new int [3];
intArray1 = new int []{0,1,2};
多维数组的初始化:
二维数组:int a[][] = {
{1,2}, {3,4,5,6}, {7,8,9}};
HashMap
定义格式:
HashMap<String, String> map = new HashMap<>();
总结:增 — map.put(“4”, “c”);
删 — map.remove(“4”);
改 — map.put(“4”, “d”);
查 — map.containsKey(“2”);
map.containsValue(“b”);
map.getOrDefault(“key”, “defaultValue”); // 如果不存在key返回指定的值
遍历HashMap
-
For-Each
public static Object getKey(HashMap map, Object value){
ArrayList keyList = new ArrayList<>();
for(Object key: map.keySet()){
if(map.get(key).equals(value)){
keyList.add(key);
}
}
return keyList;
}
(2) 迭代器
public static Object getKey(HashMap map, Object value){
ArrayList<Object> keyList = new ArrayList<>();
Set <Object> keys = map.keySet();
Iterator<Object> it = keys.iterator();
Object key;
while (it.hasNext()) {
key = it.next();
if(map.get(key).equals(value)){
keyList.add(key);
}
}
return keyList;
}
public class CommonDataStructures