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) –