java - Arraylist of Objects - How do I remove an entry? -
i doing homework , states have able remove entry arraylist of objects.
i try check if contains user id want remove objects cannot contain strings
public class employeeprogramview extends frameview { arraylist <information> database = new arraylist <information>(); private void exitbuttonactionperformed(java.awt.event.actionevent evt) { system.exit(0); } private void addbuttonactionperformed(java.awt.event.actionevent evt) { information a; string id, firstname, lastname, startdate, annualsalary, linedup; id = idinput.gettext(); firstname = firstnameinput.gettext(); lastname = lastnameinput.gettext(); startdate = startinput.gettext(); annualsalary = salaryinput.gettext(); linedup = (firstname+" "+lastname+" "+annualsalary+" "+startdate); = new information (id, firstname, lastname, startdate,annualsalary); database.add(a); system.out.println(linedup); } private void listbuttonactionperformed(java.awt.event.actionevent evt) { string temp=""; (int x=0; x<=database.size()-1; x++) { temp = temp + database.get(x).id + " " + database.get(x).first + " " + database.get(x).last + " " + database.get(x).start + " " + database.get(x).salary + "\n"; } finaloutput.settext(temp); } private void removebuttonactionperformed(java.awt.event.actionevent evt) { string removeid; removeid = idinput.gettext(); if (database.contains(removeid)){ database.remove(removeid); } else { finaloutput.settext("no such id in database"); } } class information{ string id, first, last, start, salary; information(string _id ,string _first, string _last, string _start, string _salary){ id = _id; first = _first; last = _last; start = _start; salary = _salary; } }
}
so question is? how remove object?
the remove method remove object array long .equals returns true when comparing object , object asking remove. true identical strings; other objects defaults equal (still, can add object array , retain variable referring it).
you can remove by array index.
Comments
Post a Comment