0

In JavaScript, we can flexibly define arbitrarily, highly nested arrays. For example, the following.

var arr1 = [ [0, 1], [2, 3] ];
var arr2 = [ 
 [ [0, 1], [2, 3] ], 
 [ [4, 5], [6, 7] ] 
];

Is it possible to define something like this in Java for a field of a class? The field must be able to store arbitrary dimensions/depths of nested arrays.

I was thinking about using a List of Lists.

List<List<Integer>> arr = new ArrayList<>();

However, this is in some sense, only a 2D matrix. Note that the index is significant (it has meaning) for my use case.

I suppose I can also create a Tree structure, but that might require some non-trivial work to get it right.

public class Node {
 int index; //like the index of an array i want, unique amongst nodes of same level sharing a common parent
 List<Integer> values; //the actual elements, if any
 List<Node> children; //nesting deeper
}

Any tips are appreciated.

Jane Wayne
  • 8,205
  • 17
  • 75
  • 120
  • Are you after the `var` syntax? You'll have to declare its type explicitly, with the appropriate "dimensions", `Object[]`, `Object[][]`, `Object[][][]`, etc. depending on what you need. – Sotirios Delimanolis May 11 '16 at 01:41
  • Alternatively, you can use `Object` for the variable declaration, but then you'll need a bunch of casts anyway to get anything useful out of it. – Sotirios Delimanolis May 11 '16 at 01:43
  • Yes, the `private Object arr;` field approach has occurred to me. It seems very easy to work with, but that would require type casting and checking, and it might appear the approach is a kludge rather than a disciplined one. – Jane Wayne May 11 '16 at 01:45
  • What are you actually trying to accomplish here? – chrylis -cautiouslyoptimistic- May 11 '16 at 03:09
  • @chrylis At an implementation level, to mimic the nested array in Java as I have already done for JavaScript. At a high level, these nested arrays and their indices represent state transitions from and to probabilities. If I can get away with doing something like this nested array in Java, my code from JavaScript to Java is 90% copy and paste. – Jane Wayne May 11 '16 at 03:20

2 Answers2

1

Java is a strongly typed language, you define the dimensionality of the array when you declare it. Like,

int[][] arr1 = { { 0, 1 }, { 2, 3 } };
int[][][] arr2 = { { { 0, 1 }, { 2, 3 } }, { { 4, 5 }, { 6, 7 } } };
System.out.println(Arrays.deepToString(arr2));

However, it is also possible to make an Object refer to any array (because arrays are Object instances). In the example above, note the signature is Arrays.deepToString(Object[]).

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Yes, all those points are understood. However, this `nested array` (for the use of a better term) will be declared as a field in a class and must be able to store an arbitrary depth of nested array, that are not known a priori. The dimensions are determined outside. – Jane Wayne May 11 '16 at 01:49
  • @JaneWayne Then you'll need to use `Object`, and you can use the methods in [`java.lang.reflect.Array`](https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Array.html) to work with it. – Elliott Frisch May 11 '16 at 01:54
1

1) You can do something like this (if it makes sense for you):

List<List<List<List<Integer>> arr = new ArrayList<>();

2) Also as Javascript, Java can have multi dimensional arrays

Integer[][] arr = {{1,2},{2,3}};

3) To create an Array in runtime, you can use reflection:

Integer[] array = (Integer[])Array.newInstance(Integer.class, d1);
Integer[][] array = (Integer[][])Array.newInstance(Integer.class, d1, d2);
Integer[][][] array = (Integer[][][])Array.newInstance(Integer.class, d1, d2, d3);

newInstance

4) Also you can use a library that implements multi dimensional array. Like:

Vectorz

I think the last option is the best one in your case.

Troncador
  • 3,356
  • 3
  • 23
  • 40
  • That does make sense, except for that the depth of the nesting can be different for each instance, and is not known a priori. Unless, we can use reflection or some other technique to dynamically declare the dimensions once they are determined? – Jane Wayne May 11 '16 at 01:53
  • sure you can create dynamically multi dimensional array with reflection, but I think it is to complicated and it is easier to use a library – Troncador May 11 '16 at 02:09