如何在 Java 中动态命名对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2711067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I dynamically name objects in Java?
提问by Sam Stern
Let's say I needed to make a series of String[] objects.
假设我需要制作一系列 String[] 对象。
I know that if i wanted to make a string array called "test" to hold 3 Strings I could do
我知道如果我想制作一个名为“test”的字符串数组来保存 3 个字符串,我可以做
String[] test = new String[3];
字符串 [] 测试 = 新字符串 [3];
But let's say I needed to make a series of these arrays and I wanted them to be named, 1,2, 3, 4, 5... etc. For however many I needed and I didn't know how many I'd need.
但是假设我需要制作一系列这样的数组,我希望它们被命名为 1,2,3,4,5...等等。无论我需要多少,我都不知道我需要多少需要。
How do I achieve a similar effect to this:
我如何实现与此类似的效果:
for (int k=0; k=5; k++){
String[] k = new String[3];
}
Which would created 5 string arrays named 1 through 5. Basically I want to be able to create array objects with a name detemined by some other function. Why can't I seem to do this? Am I just being stupid?
这将创建 5 个名为 1 到 5 的字符串数组。基本上我希望能够创建一个名称由其他函数确定的数组对象。为什么我似乎不能这样做?我只是愚蠢吗?
采纳答案by Mark Elliot
There aren't any "variable variables" (that is variables with variable names) in Java, but you can create Maps or Arrays to deal with your particular issue. When you encounter an issue that makes you think "I need my variables to change names dynamically" you should try and think "associative array". In Java, you get associative arrays using Map
s.
Java 中没有任何“变量变量”(即具有变量名称的变量),但您可以创建映射或数组来处理您的特定问题。当您遇到让您认为“我需要我的变量动态更改名称”的问题时,您应该尝试考虑“关联数组”。在 Java 中,您可以使用Map
s获得关联数组。
That is, you can keep a List of your arrays, something like:
也就是说,您可以保留一个数组列表,例如:
List<String[]> kList = new ArrayList<String[]>();
for(int k = 0; k < 5; k++){
kList.add(new String[3]);
}
Or perhaps a little closer to what you're after, you can use a Map:
或者也许更接近你所追求的,你可以使用地图:
Map<Integer,String[]> kMap = new HashMap<Integer,String[]>();
for(int k = 0; k < 5; k++){
kMap.put(k, new String[3]);
}
// access using kMap.get(0) etc..
回答by Stephen C
The closest you will get in Java is:
您将在 Java 中获得的最接近的是:
Map<String, String[]> map = new HashMap<String, String[]>();
for (int k=0; k=5; k++){
map.put(Integer.toString(k), new String[3]);
}
// now map.get("3") will get the string array named "3".
Note that "3"
is not a variable, but in conjunction with the map
object it works like one ... sort of.
请注意,"3"
它不是变量,而是与map
对象结合使用时,它的工作方式类似……某种意义上。
回答by Gabriel ??erbák
What you want to do is called metaprogramming - programming a program, which Java does not support (it allows metadata only through annotations). However, for such an easy use case, you can create a method which will take an int and return the string array you wanted, e.g. by acccessing the array of arrays. If you wanted some more complex naming convention, consider swtich statement for few values and map for more values. For fixed number of values with custom names define an Enum, which can be passed as an argument.
您想要做的称为元编程 - 编写 Java 不支持的程序(它仅通过注释允许元数据)。但是,对于这样一个简单的用例,您可以创建一个方法,该方法将接受一个 int 并返回您想要的字符串数组,例如通过访问数组数组。如果您想要一些更复杂的命名约定,请考虑对少数值使用 swtich 语句并为更多值使用 map。对于具有自定义名称的固定数量的值,定义一个 Enum,它可以作为参数传递。
回答by polygenelubricants
Others have already provided great answers, but just to cover all bases, Java does have array of arrays.
其他人已经提供了很好的答案,但只是为了涵盖所有基础,Java 确实有数组数组。
String[][] k = new String[5][3];
k[2][1] = "Hi!";
Now you don't have 5 variables named k1
, k2
, k3
, k4
, k5
, each being a String[3]
...
现在你没有 5 个名为k1
, k2
, k3
, k4
, 的变量k5
,每个变量都是String[3]
...
...but you do have an array of String[]
, k[0]
, k[1]
, k[2]
, k[3]
, k[4]
, each being a String[3]
.
...但是你确实有一个String[]
, k[0]
, k[1]
, k[2]
, k[3]
, k[4]
,的数组,每个都是一个String[3]
.