简体   繁体   English

如何将一维数组拆分为具有特定列大小的二维数组?

[英]How can i split an 1D array to 2D array with specific size of column?

I have a one dimensional array arr1D[i] and i want it to convert in two dimensional array arr2D[i][j] , where the size of column in 2D array should be 512 and row size can be any thing. 我有一个一维数组arr1D[i] ,我想将其转换为二维数组arr2D[i][j] ,其中2D数组中的列大小应为512,行大小可以是任何东西。 For example, if i have 1D array with size arr1D[1024] then the corresponding 2D array should be arr2D[2][512] . 例如,如果我具有大小为arr1D[1024]一维数组,则对应的2D数组应为arr2D[2][512]

How about two forloops to copy them: 两个forloops如何复制它们:

2darray[][];
size1d = 1024;
num_colums = 512;
num_rows = size1d/num_colums;
for(i = 0; i < num_rows; i++){
     for(j = 0; j < num_colums; j++){
          2darray[i][j] = 1darray[i*num_colums+j];
      }
 }

This is just pseudocode, but with a few tweeks it should work; 这只是伪代码,但几周后它应该可以工作。 hope it helps :-) 希望能帮助到你 :-)

 int len=arr1D.length(); if(len%512 !=0)) len= len/512 +1; else len=len/512; int arr2D= new int[len][512]; int k=0; for(int i=0; i<len-1; i++) { for(int j=0; j<512; j++) { arr2D[i][j]=arr1D[k]; k++; if (k==arr1D.length()) break; } } 

Well you can easily create it by doing 好吧,您可以通过以下方式轻松创建它

yourtype arr2D[][] = new yourtype[arr1D.length/2][512] // yourtype :=[int,float,..] yourtype arr2D[][] = new yourtype[arr1D.length/2][512] // yourtype :=[int,float,..]

Now all depends on you, how you are going to copy those elements of arr1d in arr2d 现在,一切都取决于您,您将如何在arr2d复制arr1d那些元素

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

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