2013-04-18 131 views
0

我有一個arrayList與一個類中的對象和其他類我得到該arrayList與所有對象,並希望添加另一個新的對象,該ArrayList結束。將對象添加到現有的ArrayList

我的代碼是在目前是這樣的:

ArrayList<Student> studentName = new ArrayList<Student>(); 
Student student = new Student(); 
student.name("Jhon"); 
studentName.add(student); 
//In another class I am getting the arrayList with existing objects 

for (Student stu : ArrayList<Student> student){ 

System.out.printLn(stu.name);//prints existing student Name 

// Here I want to add a new student name in the name arraylist at the end. 

// output should be suppose.."Jhon", "Becky". 
} 

請幫助。謝謝。

+0

您已經添加它。這是真正的問題嗎?你面臨的是哪個問題? – SJuan76

+0

從評論,它看起來像你想修改一個集合,同時迭代它...壞!!!!!! – Rich

+0

爲什麼不添加所有的學生**然後**打印它們? 'List#add()'添加在列表的末尾。 – m0skit0

回答

0

嘗試使用ArrayList.addAll(...)