| Author |
concatenate a variable to a String in a dinamic way
|
Dura Hurtado
Ranch Hand
Joined: Feb 16, 2011
Posts: 120
|
|
Hi I am trying to do the code as follows:
for (int i = 0; i < list1.size(); i++) {
if (!content.getLoadContent$i().equals(" ")) {
¿Which code could I use to append the i var to the method?
Thanks in advance.
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12926
|
|
So, you mean that the class of content has methods named getLoadContent0, getLoadContent1, getLoadContent2 etc.?
What you want is not directly possible in Java. Sometimes people who are new to programming in Java ask a question like yours because they don't know that Java has arrays and collection classes such as ArrayList and HashMap. So, the solution often is not to create "dynamic variable names", or "dynamic method names", but to use an ArrayList or HashMap to store the data, and then iterate over that using a loop.
If you really want to call methods dynamically, by creating the name at runtime, then that is possible using the reflection API. But almost certainly a better solution is to store the data in a collection class instead of trying to do difficult things via reflection.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Dura Hurtado
Ranch Hand
Joined: Feb 16, 2011
Posts: 120
|
|
|
Thanks for reply so quickly.
|
 |
 |
|
|
subject: concatenate a variable to a String in a dinamic way
|
|
|