Is there away to generate Variables' names dynamically in Java?

后端 未结 10 2162
醉酒成梦
醉酒成梦 2020-11-28 14:46

Let\'s say that I need to generate variables to hold some input from the user (I don\'t know how many they are). Without using Array, ArrayList (an

相关标签:
10条回答
  • 2020-11-28 15:12

    I haven't seen this answered yet, so I'll go for it. Write a program that just writes out Java source code. Most of it could be a template, and you would just have a loop that would write as many "string UserString003" type variables as you want.

    Yes, this is horrible. But, as you said, it's a conceptual challenge problem for homework, so as long as no one mistakes this for "good" code, it might solve the issue.

    0 讨论(0)
  • 2020-11-28 15:12

    You mean you want to generate variables named

    var0, var1, var2 and use them in your code.

    What is the difference when you use var[0], var[1], var[2], .....

    BUT

    You can generate a Java class dynamically at runtime which implements an Interface you are using in your normal code. Then you compile this class using a compiler (For example Janino) and then load the class at runtime. Than you have created a class dynamically.

    But i wonder, whether this is necessary for your usecase.

    EDIT

    I dont now for which usecase you are using this parameters but dynamic arguments you can use in Java like this example from here

    // calculate average
            public static double average( double... numbers )
            {
               double total = 0.0; // initialize total
    
              // calculate total using the enhanced for statement
              for ( double d : numbers )              
                 total += d;                          
    
              return total / numbers.length;
           } // end method average
    
    0 讨论(0)
  • 2020-11-28 15:14

    Following is the way that i have implemented and helped me to fix my solution easily without much hurdles.

    // Creating the array List

    List accountList = new ArrayList(); 
    
    
    
    
    for(int k=0;k < counter;k++){
            accountList.add(k, (String)flowCtx.getValueAt("transitId"+m));
    }
    

    Iterating the loop and adding the objects into the arraylist with the index.

    //Retrieving the object at run time with the help of the index

    String a = accountList.get(i));
    
    0 讨论(0)
  • 2020-11-28 15:17

    I think you can generate a Java class at runtime or maybe use some script engine like Beanshell to generate the variables, you can even build the class by its bytecode. But I can't see how you will use that variables in your code, you must also create the code to work with that variables, or use reflection for that...

    A naive solution:
    create a class with all variables from var000 to var999 with a getter for each... but that's not really dynamically!

    0 讨论(0)
  • 2020-11-28 15:19

    It looks like your professor is PHP-biased on the feature (Variable variables), so he was thinking if that was possible in java.

    I personally don't think that this is possible, not in the way you are proposing. What can be done is the generation of classes at runtime, using tools like Javassist to make a more powerful reflection mechanism. So you can create a class that has the variables you want (string1, string2, etc.) at runtime.

    However, don't forget that Variable variables is a really bad technique, which leads to bad code. It might be useful on very few cases, but I really don't recommend it.

    0 讨论(0)
  • 2020-11-28 15:20

    I do not know if I understood you correctly but if you are trying to use dynamically created names for your variables then yes, definitely - I am doing it like this:

    // rndRng() creates random numbers in specified range
    // this would output dynamically created variable like "name89"
    String myDynamicalyCreatedName = "name" + Utils.rndRng(0, 100);
    final UberShader $myDynamicalyCreatedName = new UberShader();
    

    As you can see the point key here is the sign "$" that basically says "create variable name from the String that is given after this sign", and that's basically it - works like a charm for me for a few years now...hope it is what you wanted and that it helps a bit solving your problem.

    0 讨论(0)
提交回复
热议问题