Dennis Grinch Dennis Grinch - 1 year ago 87
Java Question

Looking for data structure

Looking for data structure which would have these properties:

  1. Fixed size (like an

  2. Cannot contain same elements (like

  3. Ability to remove element from any position (like
    remove(int i)

  4. Add element to any position, but only if it is empty, otherwise throw exception.

Is there such data structure? Or I need to implement myself?

Answer Source

Maybe a class with an T [] and a HashSet<T>. If the HashSet contains the element to be added, it's a duplicate. If the T [] has a non-null (or whatever you flag), that element is occupied. To remove, you'd need to remove from both data structures. The size can be limited through the T [].

With this all the 4 operations would be O(1) time.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download