How do I dynamically name objects in Java?

后端 未结 4 775
夕颜
夕颜 2020-12-30 06:25

Let\'s say I needed to make a series of String[] objects.

I know that if i wanted to make a string array called \"test\" to hold 3 Strings I could do

String[

4条回答
  •  不知归路
    2020-12-30 06:41

    What you want to do is called metaprogramming - programming a program, which Java does not support (it allows metadata only through annotations). However, for such an easy use case, you can create a method which will take an int and return the string array you wanted, e.g. by acccessing the array of arrays. If you wanted some more complex naming convention, consider swtich statement for few values and map for more values. For fixed number of values with custom names define an Enum, which can be passed as an argument.

提交回复
热议问题