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
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Using a multidimensional array

Ranch Hand
Posts: 121
• Number of slices to send:
Optional 'thank-you' note:
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

author & internet detective
Posts: 41878
909
• Number of slices to send:
Optional 'thank-you' note:
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.

Landon Blake
Ranch Hand
Posts: 121
• Number of slices to send:
Optional 'thank-you' note:
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

Sheriff
Posts: 7023
• Number of slices to send:
Optional 'thank-you' note:
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?

Landon Blake
Ranch Hand
Posts: 121
• Number of slices to send:
Optional 'thank-you' note:
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

Bartender
Posts: 1844
• Number of slices to send:
Optional 'thank-you' note:
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 ]

 You don't like waffles? Well, do you like this tiny ad? a bit of art, as a gift, the permaculture playing cards https://gardener-gift.com