wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Multi Dimensional Arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Multi Dimensional Arrays" Watch "Multi Dimensional Arrays" New topic
Author

Multi Dimensional Arrays

Glen Iris
Ranch Hand

Joined: Jul 13, 2011
Posts: 164

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?


OCPJP 6, OCMJD
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
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.

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

Joined: Nov 01, 2011
Posts: 451
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----------------{__},{_}------{__}------------ {__}--------{_} ,{_}--------------{_}------- {_}-----------

John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Helen Ma wrote:It is a 3D array with only 1 2D array inside. Inside this 2D array, there are two 1 D array.

Just like to add that not only 1 2D array will be contained. It's an array hence it can have array of 2D arrays.
Glen Iris
Ranch Hand

Joined: Jul 13, 2011
Posts: 164

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.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Check if this thread helps you.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3615
    
  14

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
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: 3615
    
  14

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.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

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
Durgesh Vaishnav
Greenhorn

Joined: Oct 06, 2006
Posts: 16

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

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.









 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multi Dimensional Arrays