Amine Kandil Amine Kandil - 7 days ago 5
Java Question

Can I create more columns in the array 2D?

I want to create 2 cells in the first array, but in the second array, I want to have 4 cells.

Is that possible in Java and is it logical?

Example:

public void stack(){
int a[][] = {{2,5234},{5,33,345,45}};
}

Answer

Yes this is possible:

int[][] a = new int[2][];
a[0] = {2, 5234};
a[1] = {5, 33, 345, 45};

Or if you want to inline the entire definition:

int[][] a = new int[][]{{2, 5234}, {5, 33, 345, 45}};