开发者

How can I reference an instantiated object with only a string representation of its name?

Let's say I have an instantiated object:

private static ArrayList开发者_StackOverflow中文版<Boolean> P1SOLUTION = new ArrayList<Boolean>();

There will be similar objects such as P2SOLUTION, P3SOLUTION, etc. I want the functionality of:

Arrays.toString(P1SOLUTION);

(Which prints the array as a string). But let's say all I have is...

String myString = "P1" + "SOLUTION";

So, when I invalidly write:

Arrays.toString(myString);

I really want the String myString to reference the object P1SOLUTION in this example. How can I create this functionality?


Store your object instances in a Map. Then reference the instances by name:

Something like this:

Map myMap = new HashMap();
myMap.put("P1SOLUTION", new ArrayList<Boolean>());

Then get your instance:

String myString = "P1" + "SOLUTION";
List myList = myMap.get(myString);

Hope this will help you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜