0

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

Blebhebhe
  • 83
  • 1
  • 9
  • Possible duplicate of [Syntax for creating a two-dimensional array](https://stackoverflow.com/questions/12231453/syntax-for-creating-a-two-dimensional-array) – Aleh Maksimovich Nov 12 '17 at 16:17

2 Answers2

0

Assign 1-D arrays of 2-D array, one by one one to the other 2-D array.

Demo-http://ide.geeksforgeeks.org/306eYp

Vikash Yadav
  • 713
  • 1
  • 9
  • 29
0

For defining a two dimensional array you should modify the for-loop

for ( int i = 0; i < n ; i ++){
  be[i] = new int[arr[i].lenght];
}
imtoori
  • 570
  • 6
  • 13