File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Simple array doubt? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Simple array doubt?" Watch "Simple array doubt?" New topic
Author

Simple array doubt?

Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

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?


http://www.lifesbizzare.blogspot.com || OCJP:81%
Marcos Stoppa
Greenhorn

Joined: Jan 19, 2010
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

Joined: Aug 01, 2009
Posts: 1055

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

Joined: Oct 02, 2003
Posts: 11229
    
  16

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


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
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!


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

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
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38349
    
  23
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

Joined: Aug 01, 2009
Posts: 1055

wow good examples why didnt that clicked in my mind???
Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

I really like the port analogy

Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

cool
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38349
    
  23
well done getting the point
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Simple array doubt?