If not for the exam, please try to use array defining brackets [] after the type for better readability. int [][][] intArray - declaration will tell you directly that it's a 3d array.

Coming to the question, 3d array is nothing but an array that holds 2d array's inside it.

It is a 3D array with only 1 2D array inside. Inside this 2D array, there are two 1 D array.
To develop it ,
step1 write this----- {_____________________________________________________________}
step 2 write this-------{ ________ } , {______________ }, { __________________ } -------------
step 3 write this--------{ __,__ }----------- {_,_}------------------- {_,_}------------------------------

Thank you Helen. I still have not cracked it. Could you, using the diagram method posted above, please show how you would construct the arrays in the code below.

There are no such things as 2D, 3D or any other dimension of arrays in Java.

Java has arrays; arrays of arrays; arrays of arrays of arrays, etc.

This may seem like nitpicking, but there is an important difference. In languages where you do have 2D arrays, such as Pascal, the resulting data type really is 'square' (or a 'cube', for 3D arrays).

In Java however, since we don't have 2D arrays, but arrays of arrays, this means we can have different lengths for different arrays. Take a look:
You can imagine the resulting array to have this form:

xxx
xx
x

John Jai
Bartender

Joined: May 31, 2011
Posts: 1776

posted

0

Stephan van Hulst wrote:There are no such things as 2D, 3D or any other dimension of arrays in Java.
In languages where you do have 2D arrays, such as Pascal, the resulting data type really is 'square' (or a 'cube', for 3D arrays).

Stephan - thanks; I din't know 2D, 3D arrays will have a strict structure and there is no such in Java.

Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3370

9

posted

1

In languages like Pascal, a 2D array is just one block of memory that holds all the elements (or references to them). This means you can not allocate them in a piecewise manner, you have to know the entire size of the structure right away.

My example above showed that you can do this in Java. Java allocates a block of memory for the outer array, and each element simply is a reference to an inner array.

This has two consequences. First of all, Java arrays can do everything that multi-dim arrays can do, and more. Secondly, it's harder to check whether Java arrays are square. Let's say you're designing a Matrix class that has a constructor that takes an int[][]. Matrices are always square, but an int[][] isn't necessarily square. To check this in the constructor, you have to loop through the array and check whether each inner array has the same length.

I've written this before, and I'm sure I'll write it again. But when people talk about mutli-dimensional arrays in java, I think of eggs.

Arrays are containers of things. I like to put eggs into containers.

A one dimensional array is like an egg carton. It may be able to hold 6, 12, 18, or 24 eggs. Not all those spots need to be filled. I can re-arrange the eggs, take one out of the middle, put a new one int, etc.

Now, a two dimensional array would be something like a crate of egg cartons. The eggs don't go directly into the crate, they go into cartons, then the cartons go into the crate. Some of the cartons in the crate may only hold 6 eggs, some may hold 12, etc. The crate does not need to be full. Some of the cartons in the crate may be empty. But, I can find the 4th carton in the crate, and then the 7th egg in the carton (assuming that carton has 7 spots for eggs).

A three dimensional array might be a shipping palate full of cartons. Again, it doesn't hold eggs directly, it hold crates which hold cartons which hold eggs.

Then you can have a tractor-trailer full of shipping pallates.

Then you can have a fleet of tractor-trailors.

Then...well...then the analogy starts to break down.

The point is that at any level, you have an array that holds ONE kind of things. You would never put a lone egg in the back of a truck. If you need to find a specific egg, you need to know what truck, what palate, what crate, what carton, and what spot in the carton.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

Really a nice example of "Multi Dimensional Arrays". I like this.... Thanks Fred & Stephan.

Enthusiasm is the engine of success!!
SCJP 1.4, OCPJP6

Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451

posted

0

Hi, Glen. In the following question, you are not required to construct arrays. You are required to recognize array type. Can 1-D array be type cast to 2D array? Can an integer be assigned to an array...... ?

Explanation
A b2[1][1] is a 2D array , as well as big, so it can be assigned to big.
B. b[1][0] is a short, as well as b3. So, b[1][0] can be assigned to b3.
C. b2[0][1][1] is a 1D array, but b is a 2D array. So it cannot be assigned to b.
D. b2[0][2][1] is a 1D array, and it cannot be assigned to a short.
E. b2[1][1][0][1] is a short and can be assigned to b[1][0].
F. b2[1][1] is a 2D array and can be assigned to b.

I understand that it is not intuitive to have a 3D and 4D array.
Let me start from the basics.
Example 1:
1. short [][] s = new short[2][2]; is a 2D array.
2. s[1] is a 1D array. How do I know? It is a 2D array with 2[] 's and s[1] has one [], 2-1 = 1. Therefore, it is a 1D array.
Example 2:
1. short [][][] s = new short[2][2][2]; is a 3D array.
2. s[1] is a 2D array. How can I tell? It is a 3D array with 3[]'s and s[1] has one [], 3-1 = 2. Therefore, it is a 2D array.

Use analogy.
Imagine a 3D array short[][][] s is analogous to a building with several storeys. Each storey has several units (for each family). Each unit has several rooms. If s = new short[5][5][5]. It is similar to a building with 5 storeys, with 5 units in each of the storeys and 5 rooms in each unit.
If you are asked what the 1st storey has, you will say a 2D array with : 5 units * 5 rooms/unit.

In geometry, there is only 1 D , 2 D and 3D, not 4 D or above. But you can think of domains instead of dimension.

If we have a short [5][5][5][5] s array, I can make an analogy to a street, with 5 buildings, each of them with 5 storeys, 5 units on each storey and 5 rooms in each unit. For example, if you are asked what street "s" has, you will say 5 buildings * 5 storey/building * 5 units/storey * 5 rooms/unit.
So, if the street is actually a 4D array, it has 4 domains (building*storey*unit*room).
So, a building is actually a 3D array, it has 3 domains (storey* unit*room) and etc.