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

Using a multidimensional array

 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author & internet detective
Posts: 42134
937
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic