数组Map:(Map里面数据包含数组)本质是Map,也就是一个数组Map;
Map数组:(数组里面元素包含Map)Map数组是指数组元素是Map的数组;
测试案例:
import java.util.HashMap;
import java.util.Map;
public class day21 {
public static void main(String[] args) {
// 数组Map:(Map里面数据包含数组)本质是Map,也就是一个数组Map;
Map<String,Object> car = new HashMap<>();
String[] s = {"黑色","白色","红色"};
car.put("brand","奥迪");
car.put("colour",s);
Object[] co = (Object[]) car.get("colour");
System.out.println("数组Map:");
for (int i = 0; i < co.length; i++) {
String c = String.valueOf(co[i]);
System.out.println("数组Map:"+"brand:"+car.get("brand")+"--"+"colour:"+c);
}
System.out.println("---------------");
// Map数组:(数组里面元素包含Map)Map数组是指数组元素是Map的数组;
Map[] p1 = new Map[3]; // 先初始化,开辟内存空间;
Map<String,Object> map1 = new HashMap<>();
map1.put("name","小婷");
map1.put("age",20);
Map<String,Object> map2 = new HashMap<>();
map2.put("name","小霞");
map2.put("age",22);
Map<String,Object> map3 = new HashMap<>();
map3.put("name","小桐");
map3.put("age",24);
p1[0] = map1;
p1[1] = map2;
p1[2] = map3;
System.out.println("Map数组:");
for (int i = 0; i < p1.length; i++) {
Map p = p1[i];
System.out.println(p.get("name")+"--"+p.get("age"));
}
}
}
测试输出:
数组Map:
数组Map:brand:奥迪--colour:黑色
数组Map:brand:奥迪--colour:白色
数组Map:brand:奥迪--colour:红色
---------------
Map数组:
小婷--20
小霞--22
小桐--24