Understanding Multi-dimension Arrays Hey guys can someone explain to me how a multi-dimension array can be pictured (like the rows or col) or something in the lines of pictures as given in the K & H book if possible I am not able to get a clear understanding of the M_D arrays For e.g. something like

byte b2 [][][][] = new byte [2][3][1][2];

How can I visualize this statement

Also it would be great if someone could point me to some good literature on Multi Dimensional arrays.

I think the key to visualizing the array dec you have is to remember that there are no multi-dimensional arrays in Java. What we are commonly calling multi-dimensional arrays are really arrays of arrays.

That said, you can think of a multi-dimensional array any way that mirrors how you are using it. For example, sometimes you can think of a two-dimensional array as a Cartesian coordinate system if you needed to track the coordinates of a fly on the ceiling. You can do the same thing for 3, 4, and n- dimensions.

I have used four or more dimensional arrays to keep totals in a report. For example, I was keeping running totals for health care costs for individual employees who are part of companies that are part of insurance groups that are summarized by regions that are totaled for the whole report. For example:

double [][][][][] RunningTotals= new double [V][W][X][Y][Z]; Where V is the number of reports, which was 1. W is the number of regions covered in the query X is the number of insurance groups in the query Y is the number of companies in the query Z is the number of employees in the query

As I added a dollar amount for each employee, I added the amount to each summary bucket. As I printed each summary, I set each bucket to zero.

My point is that you don't have to picture multi-dimensional arrays the way Einstein would.

Ethan Bosco
Greenhorn

Joined: Nov 01, 2004
Posts: 15

posted

0

Thanks Borderi, For taking the time with such a detailed post but unfortunetly I still not getting it

give

what I would like to understand is the legal integer values of x1, x2, x3, x4 so that the statment would not cause a runtime expcetion.This would also help me to understand the assignment of such an array to say a 2D or 2D array. K&H book has a great picture Pg 22 , Fig 1-3 for 2D array but I am not able to extend the idea to an 4 dimensional array.

Ethan, I had the same questions about a multi dimensional array. I will try to explain what i understood. Please correct me if i am wrong.

Single dimensional array

int[] x= new int[3]; x is a reference variable to an array that can hold 3 integer values at x[0], x[1] and x[2].

Multi dimensional array int[] x=new int [3][]; x is a reference variable referring to an array that can hold references (not values like the single dimensional array) to another Single dimensional array at x[0],x[1],x[2]. i.e say that x[0][0]=1. then x[0] is holding a reference to another Single dimension array which has a value 1 at index 0; x[0][1]=2 then x[0] is holding a referece to the above Single dimension array which has a value 2 at index 1;

So far so good.

I was confused when i saw a line similar to the following line in the K & B book.

int[] x =new int[3][2]; After thinking about it for a while, I thougt that x is a reference variable holding refereces at x[0], x[1] and x[2] to other single dimensional arrays with the number of indices as 2 for each array.

i.e x[0] would hold a reference to an array object that will have values at x[0][0] and x[0][1]

x[1] would hold a refernce to an array object that will have values at x[1][0] and x[1][1]

similarly x[2] would hold a reference to an array object that will have values at x[2][0] and x[2][1].

To summarize

The differece between the statements int[][] x = new int[3][] and int[][] x=new int[3][2] is that the first statement does not specify the number of indices of the arrays which are reffered by either x[0], x[1] or x[2] where as the second statement specifies the number indices to each of the array referred by x[0], x[1] and x[2] as 2.

Excuse me if i am wrong or if i furter added to your confusion. All the Gurus out there please correct me if i am wrong.

lak naidu
Greenhorn

Joined: May 16, 2003
Posts: 12

posted

0

No one has posted their opinion on my explanation. I am still not sure if i was right! Could some guru please take time to view and post opinion? Thank you very much.