Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

copying column of a 2D array as 1D array in java?

Let's say I have a 2D array:

int[][] a = new int[4][3];

populated such that:

1 2 3
4 5 6
7 8 9
2 5 7

Is there any shortcut method in java to extract lets say column 1 as single array:

array1 = {1 4 7 2};

Currently what I am doing is traversing through the whole 2D matrix and with if condition (if j==0), I traverse over the rows and add values to 1D array.
Just wondering if there is any standard method offered in java for such tasks.

like image 391
Johnydep Avatar asked Jun 24 '26 21:06

Johnydep


1 Answers

No there is no shortcut to doing this. You have to loop over the arrays, switching the x & y indices.

like image 154
Lawrence Dol Avatar answered Jun 27 '26 11:06

Lawrence Dol



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!