Java array phonebook problem
Go to solution
Solved by Franck,
You can always change this code to another format that might make it more sense to you this way. Technically it's does the same thing but you might understand it better that way. There is one small difference and that it is bulletproof for the comparaison like this (except possible error handling in the future).
Your current code if you delete a contact in the middle of the list you will never check the other contact after that deleted contact as you will find a null contact and get out immediately.
This version will check all contacts and will get out under 2 conditions, If it finds the contact in the array or if it iterated the whole array and found nothing.
public boolean existeContacto(Contacto c) { bool contactFound = false; for (int i = 0; i < persona.length; i++) { // if the contact at the index is not null check if it's the same if (persona[i] != null && persona[i].getNombre().equals(c.getNombre())) { // if it's the same mark as found contactFound = true; // break out the loop so it return immediately without bothering checking for the other contacts break; } } return contactFound; }
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now