構造函數中的代碼不會被調用。我必須調用一個額外的函數來調用代碼。無法在構造函數中添加到ArrayLists
測試代碼:
public static void main(String[] args){
SourceClass myClass = new SourceClass();
myClass.Populate();//If this line is removed it does not work
System.out.println(myClass.GetDescGroup());
}
:被測試代碼:
public class SourceClass {
static ArrayList<String> line = new ArrayList<String>();
static ArrayList<ArrayList> description = new ArrayList<ArrayList>();
static ArrayList<ArrayList> descGroup = new ArrayList<ArrayList>();
public void SourceClass() {
//Does not work.
String testLine = new String("1 v1 v2 v3");
String[] testLineSplit = testLine.split(" ");
for (int i = 0; i < testLineSplit.length;i++) {
line.add(testLineSplit[i]);
}
description.add(line);
descGroup.add(description);
}
public void Populate() {
//works
String testLine = new String("1 v1 v2 v3");
String[] testLineSplit = testLine.split(" ");
for (int i = 0; i < testLineSplit.length;i++) {
line.add(testLineSplit[i]);
}
description.add(line);
descGroup.add(description);
}
public String GetDescGroup() {
return descGroup.get(0).get(0).toString();
}
}
相同的代碼進去填充()和SourceClass()
當一個函數被調用,以獲得的ArrayList它只是變成空的
向我們展示完整的代碼..它在哪裏工作?它不工作在哪裏? – TheLostMind
你爲什麼想要ArrayList? –
bmargulies
構造函數在哪裏? – nafas