在java中创建具有特定名称的动态对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19526359/
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
Create Dynamic Object with specific name in java
提问by user2908533
I have one test input file in which for different scenario different number of object should be created.
我有一个测试输入文件,其中应该为不同的场景创建不同数量的对象。
For example: for one test input their must be 3 object to create with name v0, v1, v2 while for other test input their must be 5 object to create with name v0, v1, v2 , v3, v4.
例如:对于一个测试输入,它们必须是 3 个对象才能创建,名称为 v0、v1、v2,而对于其他测试输入,它们必须是 5 个对象才能创建名称为 v0、v1、v2、v3、v4。
For static program of 5 object is given below:
下面给出了 5 个对象的静态程序:
Vertex v0 = new Vertex("a");
Vertex v1 = new Vertex("b");
Vertex v2 = new Vertex("c");
Vertex v3 = new Vertex("d");
Vertex v4 = new Vertex("e");
Vertex v5 = new Vertex("f");
I want to make it dynamic something like this for k=5 (no. of object):
对于 k=5(对象数),我想让它像这样动态:
for(int i=0;i<k;i++){
Vertex vi= new Vertex("str");
}
采纳答案by A4L
What you need is a Map<String, Vertext>
你需要的是一张地图<String, Vertext>
String arr = new String[]{"a", "b", "c", "d", "e", "f"};
Map<String, Vertex> map = new HashMap<>();
for(int i = 0; i < arr.length; i++) {
map.put("v" + i, new Vertext(arr[i]));
}
Then you can retrieve objects using their names, for example if you need v3
you could just write:
然后您可以使用对象的名称检索对象,例如,如果您需要,v3
您可以只写:
Vertex v3 = map.get("v3"); // returns the object holding the String "d"
System.out.println(v3.somevariable);
If somevariable
is holding the string you pass in the constructor then the output of print statement will be
如果somevariable
正在保存您在构造函数中传递的字符串,则打印语句的输出将是
d
回答by bstempi
You could use a Map
, where the key is a String
(name), and the value is the Vertex
.
您可以使用 a Map
,其中键是String
(名称),值是Vertex
。
E.g.:Map<String, Vertex>
例如:Map<String, Vertex>
You could then do this:
然后你可以这样做:
Map<String, Vertex> testObjs = new HashMap<String, Vertex>();
for(int i = 0; i < k; i++)
testObjs.put("v" + String.valueOf(i), new Vertex(i));
// The names would be like v1, v2, etc.
// Access example
testObjs.get("v1").doVertexStuff();
回答by Mateusz
It's impossible to do it in plain Java. You should be able to achieve it somehow using ASM or some byte code manipulation library, but it isn't worth the effort. The best way to do it is to use a Map
. Note that Map
is an interface, HashMap
is its implementation.
用普通的 Java 是不可能做到的。您应该能够使用 ASM 或某些字节码操作库以某种方式实现它,但这不值得付出努力。最好的方法是使用Map
. 注意Map
是一个接口,HashMap
是它的实现。
String[] names = {"v1", "v2", "v3"};
String[] constructorArgs = {"a", "b", "c"};
Map<String, Vertex> map = new HashMap<String, Vertex>();
for (int i = 0; i < names.length; i++)
{
map.put(names[i], new Vertex(constructorArgs[i]));
}
for (int i = 0; i < names.length; i++)
{
Vertex v = map.get(names[i]);
//do whatever you want with this vertex
}
You can access the variables using their names via map.get(name)
.
您可以通过map.get(name)
.
For more info on ASM see this answer.
有关 ASM 的更多信息,请参阅此答案。