简体   繁体   中英

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

I have an array list where each element of it is a double[1][3] list. Now if the array list contains n members, I want my output to be a list of size 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:

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. It all works under requirement that first dimensions of arrays in list have size equal to one.

// temp is a list?

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. 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. 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. All you need now is to copy content from temp to testList , like this:

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.

Note on naming: testArray is actually a list, while testList is actually an array.

ArrayList<double[][]>() : means a single dimension array in which each element is a double dimension array.

There are multiple double[][] elements.

If you want a single element, then you can get it with .get(index) method.

If you want to convert ArrayList<double[][]> to double[][][] then slightly change your code as

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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