# Multi Dimensional Arrays

posted 4 years ago

Hello,

I am having trouble with multi-dimensional arrays and would appreciate your help.

if a one dimensional array of ints can be represented as:

and a two dimensional array of ints can be represented as:

can you show me what a 3d and 4d array would look like please?

I am having trouble with multi-dimensional arrays and would appreciate your help.

if a one dimensional array of ints can be represented as:

and a two dimensional array of ints can be represented as:

can you show me what a 3d and 4d array would look like please?

OCPJP 6, OCMJD

John Jai

Rancher

Posts: 1776

posted 4 years ago

If not for the exam, please try to use array defining brackets [] after the type for better readability.

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

I have tried to put a code snippet - if you see the 3d and 2d arrays contain arrays within it.

`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.

`int[][][] threeDArray = {{{1,2},{3,4}},{{5,6},{7,8}}};`I have tried to put a code snippet - if you see the 3d and 2d arrays contain arrays within it.

__Output:__
Helen Ma

Ranch Hand

Posts: 451

posted 4 years ago

3D array may look like this:

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--------{ __,__ }----------- {_,_}------------------- {_,_}------------------------------

4D array look like this :

To develop it,

step 1 write this-----{ ____________________________________________________________________________}

step 2 write this--------{______________________ }, { _________________ },{ _______________________ }-----

step 3 write this-------------{________} ,{_____ }---------{_______}, { ______ }----------{_____ },{_______ }------

step 4 write this----------------{__},{_}------{__}------------ {__}--------{_} ,{_}--------------{_}------- {_}-----------

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--------{ __,__ }----------- {_,_}------------------- {_,_}------------------------------

4D array look like this :

To develop it,

step 1 write this-----{ ____________________________________________________________________________}

step 2 write this--------{______________________ }, { _________________ },{ _______________________ }-----

step 3 write this-------------{________} ,{_____ }---------{_______}, { ______ }----------{_____ },{_______ }------

step 4 write this----------------{__},{_}------{__}------------ {__}--------{_} ,{_}--------------{_}------- {_}-----------

John Jai

Rancher

Posts: 1776

posted 4 years ago

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.

Q6 Quiz A S&B E Book

A- b2[1][1] = big;

B- b[1][0] = b3;

C- b2[0][1][1] = b;

D- b2[0[2][1] = b[1][0];

E- b2[1][1][0][1] = b[1][0];

F- b2[1][1] = b;

Answers: A,B,E and F are correct.

Q6 Quiz A S&B E Book

A- b2[1][1] = big;

B- b[1][0] = b3;

C- b2[0][1][1] = b;

D- b2[0[2][1] = b[1][0];

E- b2[1][1][0][1] = b[1][0];

F- b2[1][1] = b;

Answers: A,B,E and F are correct.

OCPJP 6, OCMJD

John Jai

Rancher

Posts: 1776

Stephan van Hulst

Bartender

Posts: 6127

74

posted 4 years ago

- 2

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:

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

xxx

x

*The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.*

John Jai

Rancher

Posts: 1776

posted 4 years ago

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

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

Posts: 6127

74

posted 4 years ago

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

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.

posted 4 years ago

- 1

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.

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

Helen Ma

Ranch Hand

Posts: 451

posted 4 years ago

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.

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

Glen Iris wrote:

A- b2[1][1] = big;

B- b[1][0] = b3;

C- b2[0][1][1] = b;

D- b2[0[2][1] = b[1][0];

E- b2[1][1][0][1] = b[1][0];

F- b2[1][1] = b;

Answers: A,B,E and F are correct.

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.