• 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:
  • 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

Calling Array Elements with Variables?

 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am writing a program that converts an array of string values to text in an xml format. I need a method that takes the value in the first row and column of the array, places it in the desired xml text, and then does the same with the value in the same row, but next column. When the first row has been complete, the same process would be completed on the second row.
This brings me to my question:
Is it possible to call the element of an array based on the value of int variables?
For example: I have an array of string values named testArray. I have two variables, rowCounter and columnCounter that I can manipulate to define the location of the element in testArray that I wish to call. I have a third variable, valueHolder, that I want to hold the vale of the array element I call.
Instead of "string valueHolder = string testArray [3][12]"
could I use "string valueHolder = string testArray [rowCounter][columnCounter]"?
Thanks for any info, and I'm open to other suggestions that may get this to waork.
Landon
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This

outputs this:

C:\>java -cp . Test
this element contains 0 and 0
this element contains 1 and 0
this element contains 2 and 0
this element contains 3 and 0
this element contains 0 and 1
this element contains 1 and 1
this element contains 2 and 1
this element contains 3 and 1
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Presuming rowCounter & columnCounter both have primitive integer values then there is nothing wrong with doing this.
 
fred rosenberger
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh, and you will also need to make sure that rowCounter and columnCounter dont' exceed the size of your array, or bad things will happen...
 
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 guys. I'll make sure rowCounter and columnCounter are primitive int values, and that they don't exceed the size of my array. Thanks again for the input.
Landon
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic