java - Copying a 2D array into a 1D array -


i have 2d array of 7 rows 5 cols. trying convert 2d array 1d array reason copy last element in every row or full last column.

my 2d array looks :

0   33  32  37  0  85  73  82  73  80  104 103 95  109 101  88  108 111 116 100  133 119 102 122 116  116 123 95  112 117  0   57  76  58  0  

but output of 1d array is:

0.0 80.0 101.0 100.0 116.0 117.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ..... 

here code :

public static void gettestimg() {     testimg = new double[35];     double testvalues;     (int r=0; r < testpix.length; r++) {         (int c=0; c < testpix[r].length; c++) {             testvalues = testpix[r][c];             testimg[r] = testvalues;         }     }     (int r=0; r < testimg.length; r++) {        system.out.print(testimg[r] + " ");     }     system.out.println(); } 

i've been trying work out i'm going wrong can't work out causing this. if print "testpix[r][c]" in loop printing elements in order don't know problem is. can help?

you copying wrong index of output array (r index of current row of source 2d array). need different index variable :

int x = 0; (int r=0; r < testpix.length; r++) {     (int c=0; c < testpix[r].length; c++) {         testimg[x++] = testpix[r][c];     } } 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -