我们如何将3D数组转换为Java中的1D数组?将3D数组展平为1D数组
我用波纹管代码:
input :double [][][]S_p = { { { 1.1, 2.1 }, { 3.2, 4.1 } },
{ { 5.2, 6.1 }, { 7.1, 8.3 } } };
int rows = S_p.length;
int columns = S_p[0].length;
int depth = S_p[0][0].length;
double [] d1 = new double[row*columns*depth];
for(int i=0;i<depth;i++) {
for(int j=0;j<rows;j++){
for(int k=0;k<columns;k++) {
for(int ii=0;ii<rows*columns*depth;ii++) {
d1 [ii] = S_p[ depth *rows *i + columns *k +j];
}
}
}
out put b[]= {1.1, 2.1, 3.2 , 4.1 ...}
但这不起作用
我的输入和预期输出是什么?你试过的代码有什么问题?你的问题是非常不清楚的,你的例子不能像现在这样编译。 –
你有'rows * columns * depth'单元格。没有必要迭代太深,你已经用三个第一个for循环来浏览所有单元。 – Aaron