数组是否可以存放map或者list?
其实就是一个对象数组,构建一个存放对象的数据,再把map或者list存放进去即可。
存放map:
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<String, Integer>();
Map<String, Integer> map1 = new HashMap<String, Integer>();
Map<String, Integer> map2 = new HashMap<String, Integer>();
map.put( "a", 1 );
map1.put( "b", 2 );
map2.put( "c", 3 );
Object[] intArr = new Object[3];
intArr[0] = map;
intArr[1] = map1;
intArr[2] = map2;
for (int i = 0; i < intArr.length; i++) {
Map<String, Integer> temp = (Map<String, Integer>) intArr[i];
System.out.println("temp="+temp.toString());
}
}
存放list:
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
List<String> list1 = new ArrayList<String>();
List<String> list2 = new ArrayList<String>();
list.add("a");
list1.add("b");
list2.add("c");
Object[] intArr = new Object[3];
intArr[0] = list;
intArr[1] = list1;
intArr[2] = list2;
for (int i = 0; i < intArr.length; i++) {
List<String> temp = (List<String>) intArr[i];
System.out.println("temp="+temp.toString());
}
}