Thread: ADT Map help

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    1
    Rep Power
    0

    ADT Map help


    This should be a fairly simple method to complete, but I cant seem to get it to work. All i'm trying to do is write a remove method that looks through this map, if it finds the key it needs to it removes that key and returns the value it had. If it is not found null is returned. here is what I have:

    Code:
    public V remove(K k) { 
              for (MyEntryClass<K,V> e: lst)
                          if (k.equals( e.getKey())) 
                                 lst.remove(e); 
                          return e.getValue(); 
    
              return null;
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    73
    Rep Power
    16
    You will find Java easier to work with if you use curley braces.

    Code:
        public V remove(K k) {
            for (MyEntryClass<K, V> e : lst) {
                if (k.equals(e.getKey())) {
                    lst.remove(e);
                }
                return e.getValue();
            }
    
            return null;
    
        }
    This is conditionally removing the value but it is always going to return on the first entry. Perhaps what you intended is this:

    Code:
        public V remove(K k) {
            for (MyEntryClass<K, V> e : lst) {
                if (k.equals(e.getKey())) {
                    lst.remove(e);
                    return e.getValue();
                }
            }
    
            return null;
    
        }
    Hope this helps,
    slink

IMN logo majestic logo threadwatch logo seochat tools logo