英文:
How to get a array by variable? (Java)
问题
以下是您要翻译的代码部分:
我有一堆数组
static String[] Laura= {"Female", "Blond"};
static String[] Phil= {"Male", "Brunette"};
static String[] Mark= {"Male", "Brown"};
static String[] Person= {"Gender", "Haircolor"};
我想根据用户输入替换占位符"person",使用数组2-4中的内容。
简而言之:
我想通过将用户输入的名称作为字符串来访问一个数组,并替换我的"person"数组。
所以像这样:
`person[1] = "Laura"[1]`
我有大约150个不同的数组,为此而编写150个代码片段似乎不合理,肯定有更简单的方法。 (这可能非常简单,只是我目前无法想到。)
我尝试将我的变量放在通常应该是数组名称的地方,但出现了语法错误。
我尝试过:
`person[1] = variable[1]`
英文:
I have a bunch of Arrays
static String[] Laura= {"Female", "Blond"};
static String[] Phil= {"Male", "Brunette"};
static String[] Mark= {"Male", "brown"};
static String[] Person= {"Gender", "Haircolor"};
and I want to replace the placeholder person with content from the arrays 2-4, depending on user input.
In short:
I want to acces an array by giving the name as a String that the user inputs and replace my person array.
so something like this:
person[1] ="Laura"[1]
I have about 150 different arrays, and having 150 code snippets just for this seems unreasonable to me, there has to be a way. (Its probably really simple I just cant think of it at the moment)
I tried just putting my variable in the place where the Array's name would usually be, but its a Syntax error.
I tried:
person[1] = variable[1]
答案1
得分: 1
变量名在编译后不会保留,至少不能以可用的方式保留。因此,直接回答你的问题:不可能。
鉴于这一点,Java程序员通常不会做你所做的事情。相反,他们会创建一个Map
,将字符串(比如Laura
)映射到值(比如new String[] {"Female", "Blond"}
)。
实际上,顺便说一下,Java程序员不会将这样的数据存储在字符串数组中,他们会创建类型,例如:
enum HairColor {
BROWN, BLONDE, OTHER;
}
enum Gender {
MALE, FEMALE, OTHER;
}
record Person(Gender gender, HairColor hairColor) {}
然后,你可以有一个Map<String, Person>
,将一个人的名字("Laura")映射到表示她的Person
实例。
然后,你可以执行person[1] = "Laura"[1]
,如下所示:
Person p = persons.get("Laura");
然后继续操作。要创建这个映射,你可以用以下代码替换:
String[] laura = {"Female", "Blonde"};
// 替换为
Map<String, Person> persons = new HashMap<>();
persons.put("Laura", new Person(Gender.FEMALE, HairColor.BLONDE));
英文:
Variable names do not survive compilation, at least, not in a usable way. Thus, to directly answer your question: impossible.
Given that it is, java programmers tend not to do what you're doing. Instead, they'd make a Map
that maps strings (such as Laura
) onto values (such as new String[] {"Female", "Blond"}`).
Actually, while we're at it, a java programmer would never store such data in a string array, they'd make types, such as:
enum HairColor {
BROWN, BLONDE, OTHER;
}
enum Gender {
MALE, FEMALE, OTHER;
}
record Person(Gender gender, HairColor hairColor) {}
Then you'd have a Map<String, Person>
that maps the name of a person ("Laura"
) to an instance of Person
representing her.
Then you can do your person[1] ="Laura"[1]
as:
Person p = persons.get("Laura");
and go from there. To make this map, you replace:
String[] laura = {"Female", "Blonde"};
// with
Map<String, Person> persons = new HashMap<>();
persons.put("Laura", new Person(Gender.FEMALE, HairColor.BLONDE));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论