java - How to dynamically create class objects with serial naming? -
java - How to dynamically create class objects with serial naming? -
suppose take integer user input - 17. want create 17 "node" class objects. , they'll named node1, node2, ..., node17.
how accomplish that?
don't. asking bad idea.
what can add together multiple new objects array or collection. if don't care names arraylist<node>
job. if need names create them keys in hashmap<string, node>
or similar.
public list<node> makethismanynodes(int count) { list<node> nodes = new arraylist<node>(); (int i=0; i<count; i++) { nodes.add(new node()); } homecoming nodes; }
static final string node_base_name = "node_%d"; public map<string, node> makethismanynodes(int count) { map<string, node> nodes = new hashmap<string, node>(); string key; (int i=0; i<count; i++) { key = string.format(node_base_name, i); nodes.put(key, new node()); } homecoming nodes; }
java object dynamicobject
Comments
Post a Comment