How to define a java object name with a variable?

前端 未结 3 1610
鱼传尺愫
鱼传尺愫 2020-11-30 06:40

I need to create a large number of objects using a pattern of naming easily obtainable through a loop. Is there any way to have an object name be read from a variable, like

3条回答
  •  南方客
    南方客 (楼主)
    2020-11-30 07:02

    Easily obtainable? Have you looked into Arrays? If you want to use Strings then a string array will suffice, there are also char and int arrays.

        String[] myStringArray = new String{"hello", "world"};
        int[] myIntArray = new int{1, 2, 3, 6};
    

    I suggest you read up on arrays and their uses, I believe it may solve your problem.

提交回复
热议问题