• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Arrays in Java

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a question about arrays. I'm learning about 2 dimensional arrays ( array[][] ). If I declare an array such as

does this implicitly create a 3 X 3 array? There would be three rows, of course. One row has one column, one has two columns, and one has three columns. I'm assuming that a 3 X 3 is created, and the unused columns just have null values - or aren't arrays automatically initialized with default values? I forget now. Help?

P.S. Is there such a thing as a 3 dimensional array? Other than declaring an array of 'array objects'?
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
David
Java does not have the notion of multi-dimensional arrays. What you get is actually an array of arrays. So in your example you would have an array that has, as its elements, 3 arrays. In the array number, number[0] would be an array of 2, number[1] would be an array of 1, and number[2] would be an array of 3. There is no requirement that all of the contained arrays in an array be of the same size which means you need to be careful not to throw an IndexOutOfBounds exception when accessing them.
So, for your last question, in Java there is no 3 dimensional array but you can have an array of array and each array in the first array can have arrays as there elements. Did that make sense?
Hope that helps
 
Author
Posts: 253
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
David:
Dave's post above is right. However, I would describe the situation just slightly differently.
Java does support multidimensional arrays, including 2 and 3-dimensional ones. However, it implements them as arrays of arrays, as Dave points out.
Thus, a 3-dimensional array consists of an array of 2-dimensional arrays, which consists of an array of arrays. Because of this approach to multidimensional arrays, each sub-array can be of a different length. Multidimensional arrays in which not all dimensions are the same length are sometimes called "ragged arrays".
In situations in which you aren't using a "ragged arrays", Java's multidimensional arrays act much like multidimensional arrays in other computer langauges. For example, consider this program. It loads a 3 by 4, two-dimensional table with the values 1 through 12.

It produces the following output.
1 2 3 4
5 6 7 8
9 10 11 12
As the program shows, multidimensional arrays in Java work like one would expect.
However, in your example:

You are creating a ragged array of 2 dimensions. Assuming your initialization, it looks like this
1 2
3
4 5 6
As you can see, it is a 2-dimensional array in which the rows are unequal in length. However, it is still a 2-dimensional array.
 
David Crossett
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
O.K. So what you're saying is to think of each 'row' as an array of varying lengths - like Dave said, one of length 1, one of length 2, and one of length 3 (but not in that order, of course). So, to figure out the length of these arrays, would this be correct?

This should work, right?
Also, do arrays have to be initialized to default/zero's? Because they are really objects, don't they automatically initialize themselves upon creation? Or am I mixing up instance variables vs. class variables? I forget now, but one of those auto initialize, one doesn't. Thanks guys!
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
David
You are correct in the way you'd get the lengths of the arrays. Although I don't know if I'd call them columns, I'd be inclined to just say length, but hey, that's just semantics
As far as intialization.
Arrays in classes or instances that are declared but not created with new will have the array variable initialized to null and all elements of the array will be uninitialized.

Array variables in methods behave the same as other variables - they are not initialized until constructed, however, once constructed their elements are initialized to default values

Hope that helps
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just read a book that said Java supports "non-rectangular" arrays. I think I liked "ragged" above even better.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic