programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Simple array doubt?

Ranch Hand
Posts: 1078
int a[][]=new int[3][3]

here a is an array object having 3 rows and 3 columns...

int a[][][]=new int[3][3][3]

now heres the part i didnt get i assumed that the first two square braces are representing row and column but what about the 3 rd square bracket what does it reperesent?

Greenhorn
Posts: 4
Vishal, when you're using a 3D array, you can think that's like a rubik cube. In Java, a multi dimensional array is just an array of arrays:

int a[] - An array of integers
int a[][] - An array of arrays of integers
int a[][][] - An array of arrays of arrays of integers, and so on...

The following array: int a[][][] = new int[2][2][2]

a[0][0][0] - integer
a[0][0][1] - integer
a[0][0] - array of integers, with a length of 2
a[0] - array of array (3D) of integers, with a length of 2 (means it have 2 arrays inside)

Vishal Hegde
Ranch Hand
Posts: 1078
ook do you mean when i declare

int a[][] ,it means that an array is within an array???if the array is within itself,then how come people say its a matrix format of rows and columns

lowercase baba
Bartender
Posts: 12565
49
because that is an easy way of conceptualizing it.

I try to think of muti-dimensional arrays like shipping stuff - eggs for example. a 1 dimensional array is an egg carton - it holds eggs.

a 2D array would be like a box of egg cartons. The box may have different size cartons in it, but it ONLY holds egg cartons.

Then you may have a palate of boxes of cartons of eggs.

Then you may have a shipping containter full of boxes full of cartons full of eggs.

Then you may have a ship full of containers full of boxes full of cartons full of eggs.

Then you may have a port full of ships full of containers full of boxes full of cartons full of eggs...

Ranch Hand
Posts: 710
fred rosenberger wrote:because that is an easy way of conceptualizing it.

I try to think of muti-dimensional arrays like shipping stuff - eggs for example. a 1 dimensional array is an egg carton - it holds eggs.

a 2D array would be like a box of egg cartons. The box may have different size cartons in it, but it ONLY holds egg cartons.

Then you may have a palate of boxes of cartons of eggs.

Then you may have a shipping containter full of boxes full of cartons full of eggs.

Then you may have a ship full of containers full of boxes full of cartons full of eggs.

Then you may have a port full of ships full of containers full of boxes full of cartons full of eggs...

I think that may be the best explanation of multi-dimensional arrays I have ever seen. Props to you, fred!

Vishal Hegde
Ranch Hand
Posts: 1078
wow nice example hats off ...but how to practically show ,eggs cartons,container,ships and ports as a multi dimentional array if you can plzzzzz give an example using eggs cartons ,containers etc really loved your example

Marshal
Posts: 56610
172
Marcos Stoppa, welcome to JavaRanch

Vishal Hegde, you are reminded there is no such thing as a 2D or 3D array in Java. There are arrays of arrays. A word may be made up of letters (letter[]) and a line may be an array of words (letter[][]) and a paragraph may be an array of lines (letter[][][]) and a page may be an array of of paragraphs (letter[][][][]) and a chapter may be an array of of pages (letter[][][][][]) and a book may an array of of chapters (letter[][][][][][]) and an oeuvre may be an array of of books (letter[][][][][][][]) and a library may be an array of oeuvres (letter [][][][][][][][]) . . .

and if you read a real book you will find my analogy is very inaccurate

Vishal Hegde
Ranch Hand
Posts: 1078
wow good examples why didnt that clicked in my mind???

Ranch Hand
Posts: 820
I really like the port analogy

Vishal Hegde
Ranch Hand
Posts: 1078
cool

Campbell Ritchie
Marshal
Posts: 56610
172
well done getting the point

 Don't get me started about those stupid light bulbs.