3

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.

Sophie Sperner
  • 4,428
  • 8
  • 35
  • 55
  • 4
    Arrays are *always* stored in the heap - at least notionally. They're reference types. (It's possible that very smart JVMs will perform escape analysis, but you should *assume* that it will be on the heap.) – Jon Skeet Aug 15 '12 at 18:31
  • Your arrays wouldn't be stored on the stack if they go into a collection. Java *may* try to optimize this ([link](http://stackoverflow.com/a/2099726/335858); see Jon's note at the bottom of the answer) but it does not have to. – Sergey Kalinichenko Aug 15 '12 at 18:31
  • Suggestion, 2D array are overrated: Use a pseudo-2D-array with size of length * width, and access with `[x+(length*y)]` – TheZ Aug 15 '12 at 18:32
  • Even some simple array like `int[] a = new int[5]` will be on the heap.. Alright, thanks for information, so `ArrayList` is a solution. – Sophie Sperner Aug 15 '12 at 18:33
  • @ TheZ: very interesting, would be nice to see the reasons why [x*y] linear array is better than 2D array.. – Sophie Sperner Aug 15 '12 at 18:34
  • @SophieSperner you probably wanted `new int[n][][];` – obataku Aug 15 '12 at 18:34
  • @SophieSperner a 1D array is often chosen when an array is non-jagged and rectangular as it can in cases be more efficient, but in this case your array __is__ jagged, so ignore him. – obataku Aug 15 '12 at 18:36
  • Whoops, forgot the length and typed it wrong! Fixed. Sleepy brain is not good... for clarity: capacity of `xsize * ysize`, and access with `[x+(xsize*y)]`. Note that the technique can be expanded to any size multidimensional array as needed (it just complicates the access calculation a little more) – TheZ Aug 15 '12 at 18:36
  • @SophieSperner hopefully I've answered your question! Note that `List` and `ArrayList` are __not__ meant as replacements for arrays. – obataku Aug 15 '12 at 18:39
  • @veer Often ArrayList is a better choice than using array and the performance difference is often not important. – Peter Lawrey Aug 15 '12 at 18:52
  • @PeterLawrey maybe if what you want is a `List`, then sure; my point is that they're not congruent, not that there is a performance difference. – obataku Aug 15 '12 at 18:56
  • @veer its not a drop in replacement that's for sure. There is a performance difference between `int[]` and `List` but even then it might be as much you might expect esp if the values are small. ;) – Peter Lawrey Aug 15 '12 at 18:58

2 Answers2

4

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);
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
3

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

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

--> this is invalid with syntax, you will get compiler error. Use :

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

but a is an object as arrays in java are object so a will be stored on heap not on stack.

Sam R.
  • 16,027
  • 12
  • 69
  • 122
Nandkumar Tekale
  • 16,024
  • 8
  • 58
  • 85