The code has the start of methods to remove and element from
The code has the start of methods to remove and element from the ArrayMultiSet class. You need to implement the findFirstIndex() method so that it returns the first index in the backing store where the element is found in the ArrayMultiSet. You should also implement the removeAtIndex() method to do the actions needed to remove the element from the backing store and perform any updates to the instance variables.
Solution
1) findFirstIndex() method logic.
private int findFirstIndex(Object obj) {
/**1.Loop over the array to get the first matching element using the equals method.
* Note:Your class(E) should override hashcode and equals method as it has a contact.
* 2.Return the index i if MultiSet contains given object else return -1.
*/
for (int i = 0; i < _store.length; i++){
if(_store[i].equals(obj)){
return i;
}
}
return -1;
}
2) removeAtIndex() method logic.
private boolean removeAtIndex(int indexFound) {
/**1.Loop over the array to get the first matching element using the equals method.
* Note:your class(E) should override hashcode and equals method as it has a contract.
* 2 Assign reference to null, Arrays are fixed length and can not be resized once created. You can set an element to null to remove an object reference.
* 3.return true as it is removed(making null) else return false.
*/
for (int i = 0; i < _store.length; i++){
if (_store[i].equals(_store[indexFound])){
//Array size cannot be resized so assigning reference to null.
_store[i] = null;
return true;
}
}
return false;
}
