简体   繁体   English

转换ArrayList <double[][]> 加倍[] []

[英]Convert ArrayList<double[][]> to double[][]

I have an array list where each element of it is a double[1][3] list. 我有一个数组列表,其中每个元素都是double[1][3]列表。 Now if the array list contains n members, I want my output to be a list of size double[n][3] . 现在,如果数组列表包含n个成员,则我希望输出为大小为double[n][3] Based on this post I tried the following: 根据这篇文章,我尝试了以下方法:

ArrayList<double[][]> testArray = new ArrayList<double[][]>();
double [][] testList = new double[testArray.size()][];
for(double [][] temp : testArray){
    // temp is a list?                    
}

I also tried the following: 我还尝试了以下方法:

double [][] test = new double[testArray.size()][3];
test = testArray.toArray(test);

None of them are working for me. 他们都没有为我工作。 Any help would be much appreciated. 任何帮助将非常感激。 Thanks. 谢谢。

If all arrays in list have 1st dimension size equal to one then you can try: 如果列表中的所有数组的第一维尺寸等于1,则可以尝试:

ArrayList<double[][]> testArray = new ArrayList<double[][]>();
int testArraySize = testArray.size();
double [][] testList = new double[testArraySize][];

for (int i = 0; i < testArraySize; i++) {
    testList[i] = testArray.get(i)[0];
}

I think it's pretty straightforward so I'll comment only on that: 我认为这非常简单,因此我仅就此发表评论:

get(i)[0]

This takes i -th element from list, which is a two dimensional array and then [0] operator is invoked which accesses second dimension which is what we want to copy to target array. 这从列表中获取第i个元素,该元素是二维数组,然后调用[0]运算符,该运算符访问第二维,这是我们要复制到目标数组的内容。 It all works under requirement that first dimensions of arrays in list have size equal to one. 所有这些都是在列表中数组的第一维的大小等于1的要求下进行的。

// temp is a list? // temp是一个列表?

No, it is a two dimensional array - it's a list element. 不,这是一个二维数组-这是一个列表元素。

Your second snippet have no chance of working in that case since toArray creates an array in which every array element is an element from list so three dimensional array would be created. 在这种情况下,您的第二个代码片段将无法工作,因为toArray创建了一个数组,其中每个数组元素都是列表中的一个元素,因此将创建三维数组。 But still if you would use that method then to you would have to provide one dimensional array as a parameter to that method (not two dimensional) because toArray takes and returns one dimensional array. 但是,即使您要使用该方法,也必须为该方法提供一个一维数组作为参数(不是二维数组),因为toArray接受并返回一维数组。 In your case that would be one dimensional array in which every element is a two dimensional array which results in having three dimensional array which is not what you want. 在您的情况下,这将是一个一维数组,其中每个元素都是一个二维数组,这将导致具有三维数组,这不是您想要的。

You've got a nice start going on with your for loop. for循环,您已经有了一个不错的开始。 All you need now is to copy content from temp to testList , like this: 现在,您需要将内容从temp复制到testList ,如下所示:

List<double[][]> testArray = new ArrayList<double[][]>();
double [][] testList = new double[testArray.size()][];
int pos = 0;
for(double [][] temp : testArray){
    testList[pos++] = temp[0];
}

This creates a shallow copy. 这将创建一个浅表副本。 Any modifications to values of testArray will be "visible" through testList array. testArray值的任何修改将通过testList数组“可见”。

Note on naming: testArray is actually a list, while testList is actually an array. 命名注意: testArray实际上是一个列表,而testList实际上是一个数组。

ArrayList<double[][]>() : means a single dimension array in which each element is a double dimension array. ArrayList<double[][]>() :表示一维数组,其中每个元素都是一个二维数组。

There are multiple double[][] elements. 有多个double[][]元素。

If you want a single element, then you can get it with .get(index) method. 如果只需要一个元素,则可以使用.get(index)方法获取它。

If you want to convert ArrayList<double[][]> to double[][][] then slightly change your code as 如果要将ArrayList<double[][]>double[][][]则将代码稍作更改

double[][][] testList = new double[testArray.size()][][];
for(int i=0;i<testArray.size();i++){
   testList[i] = testArray.get(i);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM