Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

data struture dictionary

Options
  • 28-07-2004 11:29am
    #1
    Closed Accounts Posts: 228 ✭✭


    hi
    I'm repeating data structures:rolleyes:
    and I'm just wondering is the following variable IntIntItem part of
    one of the java libraries or has my lecturer just defined it elsewhere?
    anyone know what it actually does?
    cheers
    DE
    __________________________-
    /**
    * Simple dictionary with int keys/elements.
    *
    * @author **(adapted from GT)
    */

    public class IntIntDictionary
    {

    public static final int NOSUCHKEY = -1;

    /**
    * Initialize the dictionary with default capacity
    */
    public IntIntDictionary()
    { this(CAPACITY);
    }

    /**
    * Initialize the dictionary with given capacity
    */
    public IntIntDictionary(int cap)
    { capacity = cap;
    D = new IntIntItem[capacity];
    }

    /**
    * Return number of items in this dictionary.
    * @return dictionary size
    */
    public int size()
    { return (top + 1);
    }

    /**
    * Return true if this dictionary is empty, false otherwise.
    * @return true/false indication of emptiness
    */
    public boolean isEmpty()
    { return (this.size() == 0);
    }

    /**
    * Return element of item bearing specified key , if any,
    * and NOSUCHKEY if no such item exists.
    * @param key: the key of sought-for item
    * @return the element corresponding to key
    */
    public int findElement(int key)
    { int i;
    for (i = 0; i <= top; i++)
    if (key == D.getKey())
    return D.getElement();
    return NOSUCHKEY;
    }


    /**
    * Insert a new item bearing the specified key/element.
    * @param key: tke key of the new item
    * @param element: the element of the new item
    */
    public void insertItem(int key, int element)
    { if (top == capacity)
    { System.out.println("Dictionary overflow!");
    System.exit(1);
    }
    top++;
    D[top] = new IntIntItem(key, element);
    }

    /**
    * Remove the ietm bearing the specified key, returning
    * its element; NOSUCHKEY returned if no such item exists.
    * Where more than one item bears the key, one such
    * chosen arbitrarily is removed.
    * @param key the key of the item to be removed
    * @return the corresponding element
    */
    public int remove(int key)
    { int i;
    for (i = 0; i <= top; i++)
    if (key == D.getKey())
    { int j;
    int temp = D.getElement();
    for (j = i; j < top; j++)
    D[j] = D[j+1];
    top--;
    return temp;
    }
    return NOSUCHKEY;
    }




    private static final int CAPACITY = 1000; // default capacity
    private int capacity; // maximum capacity
    private IntIntItem D[]; // D holds the elements
    private int top = -1; // the top element of dictionary.

    }


Comments

  • Closed Accounts Posts: 47 PhilH


    The IntIntItem is a class that your lecturer (or someone) through together. From the looks of what you have posted its is just a key-value pair in which both the key and the value are integers (hence the tongue-twister name).

    Incidentally, your lecturer scores poorly for variable naming ('D' for christ's skake!), error handling (System.exit()!!) and algorithms (this dictionary has odd behaviour if you add more than one value with the same key). Ah, shucks, that's just me being cheeky. He is presumably not trying to illustrate all of that here.


  • Closed Accounts Posts: 660 ✭✭✭naitkris


    in terms of error handling (throwing/catching exceptions), that's not needed for the exam daggeredge.

    IntIntDictionary is just using int's to show a basic dictionary ADT and is perhaps a little confusing in the regard that it has keys and elements of the same type. perhaps IntStringDictionary would be a better representation of a dictionary with integers as the keys and strings as the elements.


Advertisement