This question already has an answer here:
这个问题已经有了答案:
- Assigning variables with dynamic names in Java 7 answers
- 用Java 7中的动态名称分配变量
I am looking to do what I explain below or something similar for a program that I'm writing. Prior to this part of my program I have an array that holds the names of players that are playing the game.
我正在寻找做我下面解释或类似的事情,为一个我正在编写的程序。在我的程序的这一部分之前,我有一个数组,它包含正在玩游戏的玩家的名字。
I am wanting to create a for loop that will initialize a new array based around the number of players that there are and name that new array the name of that player.
我想创建一个for循环,它将根据玩家的数量初始化一个新的数组,并将这个新数组命名为该玩家的名字。
So you get a better idea:
所以你会有一个更好的主意:
for (int i = 0; i < nPlayers; i++) {
String name = playerNames[i];
int[] name = new int[nCategories];
}
So you can see that I am attempting to assign a new array based off of the name of something I have stored in a different array. Is there any way to do this? Or something similar, which I suppose might be better/more efficient?
所以你可以看到,我试图根据存储在不同数组中的某物的名称来分配一个新的数组。有什么办法吗?或者类似的东西,我认为哪个更好/更有效?
This is a Java program btw.
顺便说一句,这是一个Java程序。
Thanks!
谢谢!
2 个解决方案
#1
3
I want to start about by noting that Ian makes a valid point. If you want to be able to retrieve a variable dynamically using a String a map is a great candidate. Create a map that uses a String for the key and an int[] array as the value. Then you can call the int[] out of the map using your String key.
我想从伊恩的观点开始。如果您希望能够使用字符串动态检索变量,映射是一个很好的候选者。创建一个映射,使用键的字符串和int[]数组作为值。然后,您可以使用字符串键从映射中调用int[]。
Map<String, int[]> players = new Map<String, int[]>();
String myname = "myname";
int[] myInts = {0,1,2};
players.put(myname, myInts);
int[] intArrayForPlayer = players.get(myname);
However, the main advantage of using Java is that it is an object oriented language. You may want to strongly consider using classes to achieve your goal. You can create a class called Players with class variables to hold all of the data you'll need for each Player. Then you can create Player myPlayer = new Player();
objects and use them to hold all your information.
然而,使用Java的主要优点是它是一种面向对象的语言。您可能想要强烈地考虑使用类来实现您的目标。您可以创建一个名为Player的类,该类具有类变量,用于保存每个播放器所需的所有数据。然后您可以创建Player myPlayer = new Player();对象并使用它们来保存所有信息。
You could use an array or ArrayList to hold all of your player objects.
您可以使用数组或ArrayList来保存所有的播放器对象。
#2
3
There's not really a clean way to create a variable with a dynamic name like you're trying to do. Perhaps a good option would be to have a Map, and create a new key in the map for each player, with the player's name as the key, and the value as a new Int array.
没有一种清晰的方法可以创建一个具有动态名称的变量。也许一个好的选择是拥有一个映射,并在映射中为每个玩家创建一个新的键,以玩家的名字作为键,值作为一个新的Int数组。
#1
3
I want to start about by noting that Ian makes a valid point. If you want to be able to retrieve a variable dynamically using a String a map is a great candidate. Create a map that uses a String for the key and an int[] array as the value. Then you can call the int[] out of the map using your String key.
我想从伊恩的观点开始。如果您希望能够使用字符串动态检索变量,映射是一个很好的候选者。创建一个映射,使用键的字符串和int[]数组作为值。然后,您可以使用字符串键从映射中调用int[]。
Map<String, int[]> players = new Map<String, int[]>();
String myname = "myname";
int[] myInts = {0,1,2};
players.put(myname, myInts);
int[] intArrayForPlayer = players.get(myname);
However, the main advantage of using Java is that it is an object oriented language. You may want to strongly consider using classes to achieve your goal. You can create a class called Players with class variables to hold all of the data you'll need for each Player. Then you can create Player myPlayer = new Player();
objects and use them to hold all your information.
然而,使用Java的主要优点是它是一种面向对象的语言。您可能想要强烈地考虑使用类来实现您的目标。您可以创建一个名为Player的类,该类具有类变量,用于保存每个播放器所需的所有数据。然后您可以创建Player myPlayer = new Player();对象并使用它们来保存所有信息。
You could use an array or ArrayList to hold all of your player objects.
您可以使用数组或ArrayList来保存所有的播放器对象。
#2
3
There's not really a clean way to create a variable with a dynamic name like you're trying to do. Perhaps a good option would be to have a Map, and create a new key in the map for each player, with the player's name as the key, and the value as a new Int array.
没有一种清晰的方法可以创建一个具有动态名称的变量。也许一个好的选择是拥有一个映射,并在映射中为每个玩家创建一个新的键,以玩家的名字作为键,值作为一个新的Int数组。