如何通过变量获取数组? (Java)

huangapple go评论58阅读模式
英文:

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&lt;String, Person&gt; that maps the name of a person (&quot;Laura&quot;) to an instance of Person representing her.

Then you can do your person[1] =&quot;Laura&quot;[1] as:

Person p = persons.get(&quot;Laura&quot;);

and go from there. To make this map, you replace:

String[] laura = {&quot;Female&quot;, &quot;Blonde&quot;};

// with
Map&lt;String, Person&gt; persons = new HashMap&lt;&gt;();
persons.put(&quot;Laura&quot;, new Person(Gender.FEMALE, HairColor.BLONDE));

huangapple
  • 本文由 发表于 2023年2月9日 02:36:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75390304.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定