Dennis Grinch Dennis Grinch - 4 months ago 9
Java Question

Looking for data structure

Looking for data structure which would have these properties:


  1. Fixed size (like an
    Array
    ).

  2. Cannot contain same elements (like
    HashSet
    ).

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

  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

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.