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
3 ответов
+ 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] );
}
}
+ 2
int [][] arr = new int[3][];
This means that you can have 3 rows but as many columns you want.
0
Yes , exactly