Search code examples
javadynamiccode-generationnames

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


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 (and other kind of lists and maps) can my code generate (lets say) String variables X times with names like (String var001, String var002, String var003, etc)? If yes, please provide sample code.


Solution

  • 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));