-3

I have an array of Contact objects that has a MAX of 50 Contacts, but will have much less, so the array is initialized with a size of 50. But I need my method to remove the Contact and shift everything after it up. What I have seems to work at times, but not every time.

   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;

}

  • 1
    perhaps you should consider using a Java Collection e.g. ArrayList – planetjones Apr 09 '13 at 07:05
  • duplicates - http://stackoverflow.com/questions/642897/removing-an-element-from-an-array-java and http://stackoverflow.com/questions/112503/how-do-i-remove-objects-from-an-array-in-java – rajesh Apr 09 '13 at 07:05
  • I have about 30 other methods that are all using an Array, so I cannot change it now. – Stephen Frazier Apr 09 '13 at 07:18

7 Answers7

1

Arrays a fixed size you cannot resize them. ArrayList on the other hand auto resize each time you add a element.

So if I have a Array of 5 I can put 5 items in it, no more no less. One thing you can do is set objects in the Array to be null or 0.

Edit: With regards to your comment, just sort the Array. Look up a easy bubble sort algorithm in Java.

Joban
  • 1,318
  • 7
  • 19
  • Yes, but the rest of the Array are just null references. When one is removed I want everything after it to just shift down, so all of the actual Contacts are at the beginning of the Array, and the rest are just null. – Stephen Frazier Apr 09 '13 at 07:12
1

try

    System.arraycopy(contactList, contactIndex + 1, contactList, contactIndex, contactList.length - contactIndex - 1);

Note that System.arraycopy is the most efficient way to copy / move array elements

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
0

your code would give exception at numContacts'th iteration since i+1 will go beyond size of array.

   for(int i = contactIndex; i < numContacts-1; i++) 
          {
              Contact_List[i] = Contact_List[i + 1]; 
          }
    Contact_List[Contact_List.length-1] = null;

Ps: its a very bad practice to use Array in such scenario, consider using ArrayList instead.

Ankit
  • 6,554
  • 6
  • 49
  • 71
0

Why don't you convert your array into a List and use the remove(Object o) method that does exactly what you describe?

It would save you some time and some testing.

Jean Logeart
  • 52,687
  • 11
  • 83
  • 118
0

use collection rather than array so that you dont have to do all the shifting processes! collection automatically shifts the elements and you dont have to worry about it!

you may do as follow,

ArrayList<Contact> list=new ArrayList<Contact>();
Contact c=new Contact();

Contact.Add(Contact);
Contact.remove(Contact);

and any more behaviours are available in ArrayList!

you may write you remove method as follows

public Contact remove(String lstnm)
  {
       Contact c=new Contact(1stnm);
       Contact contactToBeRemoved=list.get(1);
       List.remove(c);
        return contactToBeRemoved;
  }

but you have to override the equal() and compareTo() method of the object class in the Contact class! otherwise nothing will work properly!

Nomesh Gajare
  • 855
  • 2
  • 12
  • 28
0

for such purpose use ArrayList

ArrayList<Contact> array = new ArrayList<Contact>(50);

creates a dynamic array with initial capacity of 50 (this can increase as more elements gets added to the ArrayList)

array.add(new Contact());
array.remove(contact); //assuming Contact class overrides equals()

ArrayList internally maintains an array and does re-sizing, restructuring as the elements are added or removed from it.

You can also use Vector<Contact> which is similar data-structure, but thread safe.

sanbhat
  • 17,522
  • 6
  • 48
  • 64
0

Array's become pretty useless when you know how to use arrayList, in my opinion. I suggest using arrayLists. ArrayList tutorial

do like this when creating ht econtact arrayList:

import java.util.ArrayList;

public static void main(String args[]){
ArrayList<Contact> contacts = new ArrayList();

contacts.add(new Contact()); }

Use arrayLists, its the best way. Read tutorials, the are plenty of them. I suggest it cause arralist are dynamic, that means you can add and remove items and it resized itself for you.

Hope I could help even if my answers isnt very complete

Fabiotocchi
  • 226
  • 3
  • 11