• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Multi Dimensional Arrays

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Could someone explain a little to me about multi-dimensional arrays? For example, what does this code say exactly.
int ia [][] = { {1,2}, null };
Thx
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, according to the Java specification, Java has no "multidimensional" arrays. The Java language only has "arrays of arrays".
Declaring such an array can be done like the following:
int ia[][] = { {1,2,3}, {4,5,6} };
The above declaration creates ia[2][3].
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can think of a 2 demensional array as a Array which has another array inside it.
So:
This means basically you have a 2 dimensional array called "ia"
you are predefining the values which will be heald in the array.
Remember a array within a array. {1,2} is index 0 of the bigger array and on the inside 1 is index 0, while 2 is index 1. I hope this is making sense...
the null basically is index 1 of the bigger array, and being null there is not anything there.
anyway..Im sure I probably confused you more than anything...Sorry If i did..hope i helped a little :P
Cheers
Faisal
 
Jim Croce
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thx, I think it is making a little more sense. So in the example that Eric used.
The bigger array will contain 2 elements [0][1],
the [0] element will contain an array with [0][1][2] that will have values of 1,2,3 and the [1] will contain an array of [0][1][2] with the values of 4,5,6.
Is this correct or am I totally off.
Thx
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for int ia [][] = { {1,2}, null };
you will get:
ia[0][0] = 1
ia[0][1] = 2
 
Jim Croce
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thomas,
Would there be an element in [1][0] and [1][1] that will contain null?
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jim,
From your last reply, it seems you still are confused. Erik is correct in that Java has no multidimensional arrays. What that means is that arrays in java don't have to be square (i.e., same number of columns in every row). Think of multidimensional arrays in java as vectors of vectors. You example will give you the following memory situation:
ia[0][0] --> 1
ia[0][1] --> 2
ia[1] --> null
In other words, we get a triangular array. First row contains 2 columns while the second contains no columns. To see what I am talking about play with the code below.

Enjoy,
Manfred.
 
Jim Croce
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thx for your help. That makes sense. My next question was actually if the arrays have to have the same number of elements.
Thx
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic