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.

5
  • 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. Commented May 11, 2016 at 1: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. Commented May 11, 2016 at 1: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. Commented May 11, 2016 at 1:45
  • What are you actually trying to accomplish here? Commented May 11, 2016 at 3: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. Commented May 11, 2016 at 3:20

2 Answers 2

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[]).

Sign up to request clarification or add additional context in comments.

2 Comments

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.
@JaneWayne Then you'll need to use Object, and you can use the methods in java.lang.reflect.Array to work with it.
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.

2 Comments

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?
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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.