1

I tried to find a solution for this through many sources. But in vain. I have 3 2D array objects:

double[][] W1 = new double[5][10];
double[][] W2 = new double[2][3];
double[][] W3 = new double[4][6];

and I want another array object to contain those 3 array objects. How do I do it?

I found that it is possible to make many 1D arrays to an array of 2D arrays using the following

double[][] W = new double[][]{W1, W2, W3}; //provided W1, W2, W3 are 1D arrays.

But how do I do the same for 2D arrays?

2
  • double[][][] W = {W1, W2, W3}; Commented Nov 13, 2015 at 16:31
  • Your array W is not an array of 2D arrays, it is a 1D array of 1D arrays, also known as a 2D array. Similarly, it sounds like you don't really want a 2D array of 2D arrays, you actually want a 3D array that's initialized with 3 2D arrays. In either case, the end result is just a single array. Commented Nov 13, 2015 at 16:35

2 Answers 2

6

A 1D array is declared as double[], i.e. [] of double.

A 1D array of 1D arrays (a 2D array) is declared as double[][], i.e. [] of double[].

A 1D array of 2D arrays (a 3D array) is declared as double[][][], i.e. [] of double[][].

A 1D array of 3D arrays (a 4D array) is declared as double[][][][], i.e. [] of double[][][].

There is no practical distinction between a 3D array, a 1D array of 2D arrays and a 2D array of 1D arrays; or between a 4D array, a 1D array of 3D arrays and a 2D array of 2D arrays. It's purely how you choose to think about them.

I really can't think why you'd want to use arrays nested this deeply; I'd suggest you think about whether a better alternative might be exist.

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

Comments

2

So you want to initialize a 3D array from 3 2D arrays. That should do it

double[][][] W = {W1, W2, W3};

Note how you don't need new.

2 Comments

I dont want W to be a 3D array. it has to be a 2D array. I am basically a python programmer, and it is possible to do such stuff in python.. I wanted to know if the same is possible in java.
You want a 2D array where each element is a 2D array of doubles ? That would be a 4D array then

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.