我有一个联系人对象数组,其 MAX 为 50 个联系人,但数量会少得多,因此该数组的初始化大小为 50。但我需要我的方法来删除联系人并在它之后移动所有内容。我所拥有的似乎有时有效,但并非每次都有效。
public Contact remove(String lstnm)
{
int contactIndex = findContactIndex(lstnm); // Gets the index of the Contact that needs to be removed
Contact contactToBeRemoved;
if(contactIndex == -1) // If the Contact is not in the Array
{
contactToBeRemoved = null;
}
else
{
contactToBeRemoved = Contact_List[contactIndex]; // Assigns the Contact that is going to be removed
for(int i = contactIndex; i < numContacts; i++) // From where the Contact was removed to the last Contact in the list
{
Contact_List[i] = Contact_List[i + 1]; // Shift all of the Contacts after the one removed down
}
numContacts -= 1; // One Contact is removed from the total number of Contacts
}
return contactToBeRemoved;
}