Implementing retainAll in Collections


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Implementing retainAll in Collections

  1. #1
    Join Date
    Oct 2002
    Posts
    25

    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!"

  2. #2
    Join Date
    Dec 2002
    Posts
    83
    I like manipulating things like ArrayList much more than primitive arrays. In this case especially since you don't know how big the resulting array is going to be. I'd make a local ArrayList, add the items that are in theItems and newArray to it, then convert the ArrayList to an Object[] and set theItems to your new array.

    Code:
    public boolean retainAll(MyCollection c){
     
    Object[] newArray = c.toArray;
      ArrayList retainedList = new ArrayList();
      boolean wasModified = false;
    
      for(int i = 0; i < numItems; i++){
        for(int j = 0; j < c.size(); j++){
          if(theItems[i].equals(newArray[j]))
            retainedList.add(theItems[i]);       
    	break;
          }
        }
      }
      
      if(retainedList.size != theItems.length) {
        wasModified = true; // different size means it was modified
      }
     
      setTheItems(retainedList.toArray()); // assumes you have a setter for theItems
     
      return wasModified;
    }
    I don't know if you aren't supposed to use a Collection (ArrayList) in your code for this stuff based on the assignment. If that's the case it blows this example out of the water.

    As noted, I'm assuming there is a setter for theItems called setTheItems(). I try to stick to the practice of only modifiying a field value in its setter. So if a field has to be changed, like theItems in this case, don't directly modify the field theItems, send the new value into setTheItems().

    You probably already got this one working anyway.
    -- Steven

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center