public void addStudent() throws StudentEmailException {
if (isStudentIdInLinkedList(idTextField.getText()) == true) {
JOptionPane.showMessageDialog(null, "Error: student ID is already in the database.");
} else {
StudentDatabase addnewmodule = new StudentDatabase();//creates object for class
ArrayList finalarray = new ArrayList(); // new arraylist to store 4 (module and marks) object
finalarray.add(addnewmodule.addModule());//ADDS THAT TO THE STUDENT OBJECT
Students stud = new Students(idTextField.getText(), nameTextField.getText(), "", finalarray);
studentLinkedList.add(stud); //STUDENT OBJECT IS ADDED TO THE LINKEDLIST
displayAll();
}
}
学生ID为12的输出:
Name : dsfdsf Student Result=[[Module Name : dsfsf Marks : 1, Module Name : dsff Marks : 3, Module Name : dsfsf Marks : 23, Module Name : sdfdf Marks : 34]]
我想删除第一个模块标记,然后将另一个添加到Student对象中的finalarray ArrayList中。我知道如何检索给定学生ID的最终数组,但无法更新它。 ArrayList.remove()
似乎不起作用。
答案 0 :(得分:0)
如果您知道元素索引,则可以执行以下操作:
ArrayList<String> list = new ArrayList<>();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
System.out.println(list);
//Replace C with C_NEW
int index = list.indexOf("C");
list.set(index, "C_NEW");
示例-https://howtodoinjava.com/java/collections/arraylist/replace-element-arraylist/