java - Creating variables with names of strings -
i making api kind of thing school custom xml writer. have:
public document createdocument(int loops, int attr, string data[], string dataattr[][][]) { document betterdoc = documenthelper.createdocument(); element root = betterdoc.addelement("root"); (int = 0; < loops; i++) { element(object) data[i] = root.addelement(data[i]) (int i2 = 0; < attr; i++) { .addatribute(dataattr[i][i2][0], dataattr[i][i2][1]) }; } return betterdoc; }
the line want is:
element(object) data[i] = root.addelement(data[i])
i want create element same name of data[i].
i using dom4j xml .jar in this, way.
i have heard of called hashmap , if correct method, please explain how use it.
you can't create dynamic variable unlike groovy, php or javascript, can create array or reuse existing variable:
with existing variable:
public document createdocument(int loops, int attr, string data[], string dataattr[][][]) { document betterdoc = documenthelper.createdocument(); element root = betterdoc.addelement("root"); (int = 0; < loops; i++) { element _data = root.addelement(data[i]); (int i2 = 0; < attr; i++) { _data.addatribute(dataattr[i][i2][0], dataattr[i][i2][1]) }; } return betterdoc; }
with array:
public document createdocument(int loops, int attr, string data[], string dataattr[][][]) { document betterdoc = documenthelper.createdocument(); element root = betterdoc.addelement("root"); element[] _data = new element[loops]; (int = 0; < loops; i++) { _data[i] = root.addelement(data[i]); (int i2 = 0; < attr; i++) { _data[i].addatribute(dataattr[i][i2][0], dataattr[i][i2][1]) }; } return betterdoc; }
you can replace array arraylist
if prefer.
Comments
Post a Comment