gpt4 book ai didi

java - 让一个字符串代表一个数组

转载 作者:行者123 更新时间:2023-12-01 19:19:31 24 4
gpt4 key购买 nike

我有一个数组

String myArray[]={"one","two","three","four"};

在我的代码中,我使用与数组同名的字符串。是否有办法使该字符串指向数组?

String theArray = "myArray";

theArray[0];

这可能吗?

最佳答案

Is this even possible?

没有。

您可以使用Map<String, String[]>将名称映射到数组。

一个小例子:

String[] myArray = {"one","two","three","four"};
Map<String, String[]> map = new HashMap<String, String[]>();
map.put("myArray", myArray);
String[] temp = map.get("myArray");
System.out.println(Arrays.toString(temp));

将打印:

[one, two, three, four]

关于java - 让一个字符串代表一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4984883/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com