Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using a multidimensional array

 
Landon Blake
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34839
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic