Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: fixed size array of 2D int array [duplicate]

Is it possible to create an array of 2D int arrays like:

int n = 100;
int[][] [] a = new int[][] [n];

Array has a fixed length n and matrices (2D arrays) have different non-zero sizes (at least 1 x 1).

For performance I would like to store that in the stack, not like:

ArrayList<int[][]> a = new ArrayList<int[][]>(n);

which will be stored on the heap as far as I know.

like image 707
Sophie Sperner Avatar asked Sep 23 '26 11:09

Sophie Sperner


1 Answers

To create a 3D array

int n = 100;
int[][][] a = new int[n][][];

This creates 100 array of array of any dimension.

This is almost as (in)efficient as

List<int[][]> a = new ArrayList<int[][]>(n);
like image 160
Peter Lawrey Avatar answered Sep 25 '26 00:09

Peter Lawrey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!