java - 将 ArrayList 存储在 2d 数组 Java 中
问题描述
如何将 ArrayList 存储在二维数组中?
我试过这样,但它不起作用:
ArrayList<Integer> arrList = new ArrayList<Integer>();
ArrayList<Integer>[][] arr = new ArrayList<Integer>[9][9];
但它甚至不允许我声明 ArrayList-Array。
有没有办法将列表存储在二维数组中?
提前致谢!
解决方案
您不能在 Java 中创建泛型类型的数组。但这编译:
ArrayList<Integer> arrList = new ArrayList<Integer>();
ArrayList<Integer>[][] arr = (ArrayList<Integer>[][]) new ArrayList[9][9];
arr[0][0] = arrList;
为什么不能创建这些数组?根据泛型常见问题解答,由于这个问题:
Pair<Integer,Integer>[] intPairArr = new Pair<Integer,Integer>[10]; // illegal
Object[] objArr = intPairArr;
objArr[0] = new Pair<String,String>("",""); // should fail, but would succeed
推荐阅读
- javascript - 如何使用两个组件处于同一级别的前向引用来引用 dom 节点
- php - 如何获取发布请求 URL 参数(php)?
- java - java中数组的IndexOutofException的运行时错误?
- java - Java 事件系统/观察者模式
- javascript - 模块 '"./datasources"' 没有导出的成员 'DbDataSource'。环回
- javascript - 为什么使用 get 方法可以在 map 中找到以数组为键的键值对?
- python - 如何将普通数据帧转换为时间序列数据帧
- c++ - C++,错误:表达式必须具有整数或无范围枚举类型
- canvas - 在 TMapView (Delphi FMX) 中的两个坐标之间画一条线
- python - 使用mysql连接器的python中的回溯错误