I have this array :
int a[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 6, 3 }, { 8 }, { 7 } };
I want to print a new one up to 3 row : {1,2,3},{4,5,6},{6,3}
public static int[][] cut(int arr[][], int n) {
// n : is up " up to which row ? " Here is n = 3
// FIRST STEP :
// COUNTING ELEMENTS
int counter = 0;
for (int row = 0; row < arr.length; row++) {
for (int column = 0; column < arr[row].length; column++) {
counter++;
}
System.out.println(counter);
counter = 0;
}
// PRINTS 3 3 2 1 1 : OK
// SECOND STEP : I only want up to 3 row so : 3 3 2
// CREATE AN EMPTY TWO DIMENSIONAL ARRAY but how ?
//I think that I should be using a loop for but I've no idea how
int be[][] = new int[n][];
for ( int i = 0; i < n ; i ++){
be[i][counter];
// This doesn't work, I know why but I don't have other suggestion :s
}
// THIRD STEP : Putting values in the new array
}
return be;
}
Thank you