0
how to remove and update input from array? JAVA
This is my code so far https://code.sololearn.com/c5B61NSOUsn7 I asked this question before but I still don't get it. someone told me for(int i=0; i<studentList.size(); i++){ student = (Student) studentList.get(i); if ( student.getName().equalsIgnoreCase(searchedName){ //assign to the object here found = true; } } This for update I don't know how it's work and don't now how to code... Thank you guys for any help or comments!
4 odpowiedzi
+ 1
Hi ,
In this case i will add other field to student and teacher objects , the field will be identity , since you can have more the one student or teacher with the same name, so name is not good field for deleting or update.
so add this field to both objects (private String identity;) and update your code for (1. Add Studnet/Teacher) to get the value for this field.
2. Remove Student/Teacher
this should be done by searching the student/teacher you would like to delete/update, searching should be by identity.
it is more easy to perform this actions on collections the standard array, so it is better to create ArrayList
insted of :
student[] studentArr = new student[numbers];
teacher[] teacherArr = new teacher[numbers];
use :
ArrayList<student> studentsList = new ArrayList();
ArrayList<teacher> teachersList = new ArrayList();
so if for example you would like to delete student , you should ask the user to input the identity of the student he would like to delete then find the index of the student object with the required identity value inside the studentsList, and delete this object at this index.
string identity = scn.next();
for (student st : studentsList ) {
if(st.identity == identity)
{
studentsList.remove(st);
}
}
you can read more about collections and how to use them at sololearn java course
0
Hi Vitti!
yes I want to remove an item that user inputted in an array and update an item as well.
but I don't know how to use the add/remove method.
Thank you for the comment!