Implementing retainAll in Collections
Hi all,
need some advice on the feasability of this code. I am trying to implement the retainAll method from the Java Collections Framework. The method accepts a MyCollection 'c' and returns a boolean value. It should remove all of the objects in a bag except for the items in 'c'
public boolean retainAll(MyCollection c){
Object[] newArray = c.toArray;
for(int i = 0; i < numItems; i++){
boolean removeItem = true;
for(int j = 0; j < c.size(); c++){
if(theItems[i].equals(newArray[j]))
removeItem = false;
}//End of for loop j
//My idea with the next if statement is to remove the item that was not contained in 'c' and replace it with the last item in the Bag (Thereby avoiding null values within the bag). I then set the value of the last item in the Bag to null and decrement the number of items. I then decrement 'i' so that I can check the same index on the next loop of i.
if(removeItem = true){
theItems[i]= theItems[numItems - 1];
theItems[numItems -1] = null;
numItems --;
i--;
}//End of if
}//End of for loop i
if(numItems = 0)
return false;
}//End of method retainAll()
Biggest Question: Can I decrement 'i' within the loop. If not how would I ensue that the new value in that index gets checked against 'c'.
"I hope that one day I will be able to answer someone elses questions!"
Bookmarks