• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling Array Elements with Variables?

 
Landon Blake
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • 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
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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
 
eammon bannon
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic