0

can i initialize an array to give different length of inner array i.e int [][] a = new int[2][_] ; here blank space for dif leng

I mean int[][] a = { {1, 2, 3}, {4, 5, 6, 9}, {7}, }; See here all inner arrays have different lengths Can I achieve it in this given form I.e. int [][] a = new int[2][_ this blank space for achieve different lengths of inner arrays] ; https://code.sololearn.com/c3mp1Ob4vHHG/?ref=app

18th Dec 2019, 6:18 AM
VikaSh YadaV
VikaSh YadaV - avatar
3 Respostas
+ 1
// just leave empty space there new int[5][]; import java.util.Arrays; public class Array_diff_2Dlength { public static void main(String[] args) { int[][] arr = new int[5][]; int[] a; for (int i=0; i<arr.length; i++) { //initialise an example 2d array a = new int[i+1]; for (int j=0; j<a.length; j++) a[j] = j; arr[i] = a; } System.out.println( Arrays.deepToString(arr) ); System.out.println( arr[2][2] ); } }
18th Dec 2019, 7:36 AM
zemiak
+ 2
int [][] arr = new int[3][]; This means that you can have 3 rows but as many columns you want.
18th Dec 2019, 7:46 AM
Avinesh
Avinesh - avatar
0
Yes , exactly
18th Dec 2019, 8:37 AM
VikaSh YadaV
VikaSh YadaV - avatar