我想創建一個使用for循環在我的MainActivity.java中的虛擬對象的ArrayList。這似乎是我得到我創建的每個對象的對象引用做工精細:創建新對象後ArrayList爲空
for(int i=0; i < 10; i++){
new TaskObjects("Title " + i, "Category " + i, "Description " + i, i + 10);
Log.d("For Loop", "" + i);
}
我有一個日誌(this.toString())返回10次這樣的構造函數中:
d/ARRAY:[email protected](每個不同ID)
TaskObjects類:
public class TaskObjects {
private String title;
private String category;
private String description;
private int taskPosterID;
private ArrayList<TaskObjects> tasksArray = new ArrayList<>();
public TaskObjects(String title, String category, String description, int taskPosterID){
this.title = title;
this.category = category;
this.description = description;
this.taskPosterID = taskPosterID;
tasksArray.add(this);
Log.d("ARRAY",this.toString());
}
//getters and setters
}
然而,當我嘗試返回所述陣列的尺寸,則返回0
'我嘗試返回數組的大小,它返回0.'在哪裏? – Ravi
也許你打算讓'tasksArray'靜態? – Zircon
基本上任何地方我實例化類和運行object.getTasks()。size() –