Element elementInclude1 = doc.createElement("include");
elementMethods.appendChild(elementInclude1);
elementInclude1.setAttribute("name", "T1");
Element elementInclude2 = doc.createElement("include");
elementMethods.appendChild(elementInclude2);
elementInclude2.setAttribute("name", "T2");
Element elementInclude3 = doc.createElement("include");
elementMethods.appendChild(elementInclude3);
elementInclude3.setAttribute("name", "T3");
....
在上述示例中,创建的对象(elementInclude1,elementInclude2,..)的数目是动态已知的。
运行时所需的对象数目是已知的,使用该值在循环中迭代每次迭代中创建的新对象。我怎么能做到这一点。
需要创建对象elementInclude1,elementInclude2,elementInclude3,...而动态运行时
只是为了更好地理解我使用了一些错误的做法,在下面的代码。
String noOfObj = 5;
for(int i = 1; i<=noOfObj; i++)
{
Element elementInclude+**noOfObj** = doc.createElement("include");
elementMethods.appendChild(elementInclude+**noOfObj**);
elementInclude+**noOfObj**.setAttribute("name", "T1");
}
使用数组... – shmosel
你甚至需要单独的参考变量?你在循环之外使用它们吗? – shmosel
可能的重复[如何在运行时创建对象?](http://stackoverflow.com/questions/1938482/how-do-i-create-objects-at-runtime) –