wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Using a multidimensional array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using a multidimensional array" Watch "Using a multidimensional array" New topic
Author

Using a multidimensional array

Landon Blake
Ranch Hand

Joined: Dec 04, 2003
Posts: 121
If I have a one-dimension array named "test" with 10 elements, I can define element 9 using this syntax:
test[9] = 10
How do I define or reference elements in a multidimensional array? Would I use the following syntax to call the ninth element-second dimension of a 3-Dimension array named "test"?
test[9][] = 10
Thanks for any help.
The Sunburned Surveyor
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30116
    
150

Assuming you have created the array:
int [][] test = new int[9][3];
You can define elements as:
test[9][1] = 10;
You just need to know the indexes for the row and column in that order. Make sure to remember that indexes start with zero, so the second dimension is index 1.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Landon Blake
Ranch Hand

Joined: Dec 04, 2003
Posts: 121
Thanks for the response Jeanne.
Please allow me to ask another question:
How do I define the ninth element, second dimension/column without defining or overwriting the first and third dimension/column of the ninth element?
Thanks,
Landon
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
How do I define the ninth element, second dimension/column without defining or overwriting the first and third dimension/column of the ninth element?
Why do you think they would be "overwritten" in Jeanne's example?


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Landon Blake
Ranch Hand

Joined: Dec 04, 2003
Posts: 121
Sorry Dirk,
I am either not understanding her response or struggling to explain my question. Let me try it this way. I have an array called "points" with 10 elements. The array has 3 dimensions/columns. One for the x value of the point, one for the y value of the point, and one for the z value or elevation of the point. I now want to modify the y value of the ninth point, without modifying the x and z values. How do I do this? Do I need to know the value of the x and z values so I can use them in my statement. I'm just not sure how to manipulate or reference the second, third, n'th element in a multidimensional array. How would I code this? I hope this question makes more sense.
Thanks,
Sunburned Surveyor
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

A 3-dimensional array where each dimension is 10 elements is 1000 elements, not 10. There is really no such concept as "the ninth element" in such a construct, except insofar as the ninth element of the root array contains a 2-dimensional array.
Perhaps you have a 2-dimenaional array where one dimension is length 10 and the other dimension is length 3?

Then, to modify the y point of the ninth element, you say:

This will go over eight elements in the grid and down one element and modify that point there.
However, your problems sounds as though it's not well-suited to X-dimensional arrays. You want to create an array of objects:

[ January 09, 2004: Message edited by: Joel McNary ]

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Using a multidimensional array
 
Similar Threads
Multidimensional arrays
MultiDimensional Arrays
multidimensional arrays
Multidimensional array
Constructing multidimensional arrays