java - Removing item from ArrayList using remove(<index>) or remove(<objectRef>) -
i want create program displays current staff in arraylist
before asking user input of payroll number they'd remove. user should input payroll number of 1 of 3 staff members , press enter. upon pressing enter, program should remove particular staff member array list , display entire list again (missing out staff member they've deleted obviously). if user no longer wishes remove payroll numbers, payroll number entry should 0 , should display contents of list again.
the problem i'm having remove part.
i've been recommended of 2 ways of achieving this:
this 'search' method should return either position within arraylist (so remove(<index>)
may used) or reference object (so remove(<objectref>)
may used). if staff member not found, search method should return -1 (if remove(<index>)
being used) or null (if remove(<objectref>)
being used).
however not sure how implement in java.
here file structure:
arraylisttest.java
import java.util.*; import personnelpackage.personnel; public class arraylisttest { static scanner keyboard = new scanner(system.in); public static void main(string[] args) { long searchquery; arraylist<personnel> stafflist = new arraylist<personnel>(); personnel[] staff = {new personnel(123456,"smith","john"), new personnel(234567,"jones","sally ann"), new personnel(999999,"black","james paul")}; (personnel person:staff) stafflist.add(person); { showdisplay(stafflist); system.out.print("\nplease enter payroll number search: "); searchquery = keyboard.nextlong(); searchforpayrollnumber(stafflist, searchquery); }while(!(searchquery == 0)); } private static void showdisplay(arraylist<personnel> stafflist) { system.out.print("\n------------- current staff list -------------\n"); (personnel person : stafflist) { system.out.println("payroll number: " + person.getpaynum()); system.out.println("surname: " + person.getsurname()); system.out.println("first name(s): " + person.getfirstnames() + "\n"); } } public static void searchforpayrollnumber(arraylist<personnel> stafflist, long searchquery) { long index = stafflist.indexof(searchquery);; (personnel person: stafflist) { if (person.getpaynum() == searchquery) { system.out.print("\n------------- staff member found , removed! -------------"); system.out.println("\n\nfirst name(s): " + person.getfirstnames()); system.out.println("\nsurname: " + person.getsurname()); system.out.print("\n-----------------------------------------------"); stafflist.remove(index); return; } } system.out.print("\n------------- no staff members found. program terminated -------------"); return; } }
personnel.java (in own package named personnelpackage)
package personnelpackage; public class personnel { private long payrollnum; private string surname; private string firstnames; public personnel(long payrollnum, string surname, string firstnames) { this.payrollnum = payrollnum; this.surname = surname; this.firstnames = firstnames; } public long getpaynum() { return payrollnum; } public string getsurname() { return surname; } public string getfirstnames() { return firstnames; } public void setsurname(string newname) { surname = newname; } }
public static long searchforpayrollnumber(arraylist<personnel> stafflist, long searchquery) { //long index = stafflist.indexof(searchquery); for(int = 0; < stafflist.size(); i++) { if (stafflist.get(i).getpaynum() == searchquery) { system.out.print("\n------------- staff member found , removed! -------------"); system.out.println("\n\nfirst name(s): " + stafflist.get(i).getfirstnames()); system.out.println("\nsurname: " + stafflist.get(i).getsurname()); system.out.print("\n-----------------------------------------------"); //stafflist.remove(i); return i; } } system.out.print("\n------------- no staff members found. program terminated -------------"); return -1; }
Comments
Post a Comment