File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple array doubt?

 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Marcos Stoppa
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 1077
Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12021
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
W. Joe Smith
Ranch Hand
Posts: 710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 1077
Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47274
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 1077
Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wow good examples why didnt that clicked in my mind???
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really like the port analogy

 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cool
 
Campbell Ritchie
Sheriff
Pie
Posts: 47274
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well done getting the point
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic