Results 1 to 5 of 5
- 08-18-2010, 11:35 PM #1
Member
- Join Date
- Aug 2010
- Posts
- 25
- Rep Power
- 0
Accessing array index through the result of a string concatenation
Hi all,
I was hoping someone could assist me with the following problem.
I am fundamentally calling a method from another class through the use of an object. And this method has an input paramater which is a string.
so like this,
statusTest.value.setName(...), where ... is a string.
Within the call, i am passing a string value through an index of an array,
statusTest.value.setName(statDetails[0]);
where statDetails is an array i have declared and initialised earlier.
My problem is, I also have an array declared statDetailsCool that i want to use in the same fashion but only in certain circumstances.
So i want to do something like the following,
statusTest.value.setName("statsDetails".concat(ano therArray[0])[0]);
where anotherArray has been declared and initalised and contains "Cool" in its first index.
The compiler views this as a string usage rather than an array as it expects.
Any ideas?
Thanks.
- 08-19-2010, 12:41 AM #2
I'm not totally sure I understand the question. Are you trying to pass two joined strings as a single string to a function? In that case, you're looking at a simple "a"+"b" format:
If I misunderstand your question I apologize, but that seems like what you're looking for. If not, could you provide some sample variables & input you might expect to see?Java Code:statusTest.value.setName(statDetails[0] + anotherArray[0]);
- 08-19-2010, 12:48 AM #3
Member
- Join Date
- Aug 2010
- Posts
- 25
- Rep Power
- 0
Hi Zack,
Thanks for your reply.
I am trying to do something similar.
I have an array called RegStatDetailsCool and i want to access its indexes.
Normally i would just go, RegStatDetailsCool[0...n]
But instead i want to concantenate the "RegStatDetails" part with "Cool"
"RegStatDetails".concat("Cool")[0...n].
In the end i want this to happen,
String[] anotherArray = {"Cool", "Hot"};
"RegStatDetails".concat(anotherArray[0...n])[0...n];
Hope this makes it a bit clearer.
- 08-19-2010, 12:55 AM #4
Ah, yes, I see what you're getting at now. Unfortunately, this isn't possible by conventional methods.
You have a few alternatives; instead of using a set of variables, you could use a hash map (so the keys would be something like RegStatDetails, RegStatDetailsHot, RegStatDetailsCool, and the values would be the arrays).
HashMap (Java Platform SE 6)
The other alternative, albeit horrendous, would be to use JavaScript to evaluate the expression. See here for an example: Java Programming - Evaluate String math expression [Locked]. I wouldn't recommend this method necessarily, but it's up to you.
- 08-19-2010, 01:06 AM #5
Member
- Join Date
- Aug 2010
- Posts
- 25
- Rep Power
- 0
Similar Threads
-
Which would be better List iterator or accessing through index?
By deepuhassan in forum Advanced JavaReplies: 2Last Post: 12-17-2009, 03:06 PM -
Accessing an arrayList of multiple items per index.
By scrap in forum New To JavaReplies: 12Last Post: 11-05-2009, 07:12 AM -
Database Column vs String Concatenation
By varun_33 in forum JDBCReplies: 1Last Post: 12-11-2007, 04:14 PM -
Accessing index.dat files
By vissu007 in forum NetworkingReplies: 1Last Post: 07-01-2007, 04:47 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks