aspose file tools*
The moose likes Beginning Java and the fly likes Calling Array Elements with Variables? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calling Array Elements with Variables?" Watch "Calling Array Elements with Variables?" New topic
Author

Calling Array Elements with Variables?

Landon Blake
Ranch Hand

Joined: Dec 04, 2003
Posts: 121
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

Joined: Oct 02, 2003
Posts: 11497
    
  16

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


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
eammon bannon
Ranch Hand

Joined: Mar 16, 2004
Posts: 140
Presuming rowCounter & columnCounter both have primitive integer values then there is nothing wrong with doing this.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11497
    
  16

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

Joined: Dec 04, 2003
Posts: 121
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling Array Elements with Variables?