java - How To Remove an item from an Array and move everything else down? -
this question has answer here:
- removing element array (java) [duplicate] 15 answers
i have array of contact objects has max of 50 contacts, have less, array initialized size of 50. need method remove contact , shift after up. have seems work @ times, not every time.
public contact remove(string lstnm) { int contactindex = findcontactindex(lstnm); // gets index of contact needs removed contact contacttoberemoved; if(contactindex == -1) // if contact not in array { contacttoberemoved = null; } else { contacttoberemoved = contact_list[contactindex]; // assigns contact going removed for(int = contactindex; < numcontacts; i++) // contact removed last contact in list { contact_list[i] = contact_list[i + 1]; // shift of contacts after 1 removed down } numcontacts -= 1; // 1 contact removed total number of contacts } return contacttoberemoved;
}
arrays
fixed size cannot resize them. arraylist
on other hand auto resize each time add element.
so if have array
of 5 can put 5 items in it, no more no less. 1 thing can set objects in array
null
or 0.
edit: regards comment, sort array
. easy bubble sort algorithm in java.
Comments
Post a Comment