Deleting would work like this:
/**
* Will work long as there are no two contacts with same first name. In that case
* you'll need more data to find a unique contact.
*/
private void deleteByFirstName(String firstName) {
for (Iterator<Data> iterator = contact.iterator(); iterator.hasNext();) {
Data temp = iterator.next();
// Add null checks for proper error handling.
if (temp.getFirstName().equalsIgnoreCase(firstName)) {
iterator.remove();
return;
}
}
System.out.println("No contact with first name " + firstName + " was found.");
}
You can use similar logic for displaying or editing a single contact. Just create a helper method that loops through contacts and returns the matching the contact, that you can use for both of them. Then proceed to print or edit the Data
object.