This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
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'?
-nothing important to say, but learnin' plenty-
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 would be an array of 2, number would be an array of 1, and number 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
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.
For my latest books on Java, including my Java Programming Cookbook, see HerbSchildt.com
Joined: Feb 05, 2003
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!
Joined: May 10, 2001
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