[英]An ArrayList of two dimensional arrays
I am able to declare variables dataList and data as follows: 我可以声明变量dataList和data,如下所示:
ArrayList<Double[][]> dataList = new ArrayList<Double[][]>();
double [][] data = new double[2][];
but when I do something like 但是当我做类似的事情
dataList.add(data);
I get an error saying 我说错了
The method add(Double[][]) in the type ArrayList<Double[][]>
is not applicable for the arguments (double[][])
How can I add data to dataList? 如何将数据添加到dataList?
Declare your list to contain elements of type double[][]
instead of Double[][]
: 声明您的列表包含
double[][]
类型的元素,而不是Double[][]
类型的元素:
ArrayList<double[][]> dataList = new ArrayList<double[][]>();
This works because double[][]
is not a primitive type, and hence can be the actual value for the generic type parameter for ArrayList
. 这是可行的,因为
double[][]
不是原始类型,因此可以是ArrayList
的泛型类型参数的实际值。
You can do it with a ArrayList<double[][]>
as Ted explained. 您可以使用
ArrayList<double[][]>
如Ted所述。
ArrayList<double[][]> dataList = new ArrayList<double[][]>();
The reason for the error is, double[][]
is itself an Object, whereas Double[][]
is an array of references to Double
array of references to the Double
objects. 的原因的错误是,
double[][]
是本身的对象,而Double[][]
是引用一个数组Double
到引用的数组Double
对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.