Good time of day!

They gave me this homework: there is a class OMOSet , which represents many integers. You need to write the methods add(int element) , remove(int element) . So the source code is given, in which it is necessary to add the methods. Question: the OMOSet class OMOSet already a set, then AttayList , Collection cannot be used ... Need to write a constructor and attributes? How to create a set manually?

 class OMOSet implements OMOSetView { public void add(int element) { } public void remove(int element) { } } 
  • @evgeniya, According to the rules of the forum, questions should not be limited to the decision or the completion of student assignments. Please clarify what you have done yourself and what did not work out. - Nicolas Chabanovsky

2 answers 2

First you need to determine the structure of the data storage Given that the set is not limited, the static structure in the form of a raw array will not work. Java Collection classes can not be used, because you have to bike. There is only your will: a dynamic array (add-on over a raw array), a list, a binary tree, a hash table, or something else. What specifically to choose - it depends on the requirements for the complexity of access, insertion (at the end, beginning, middle, with or without sorting).

Inspired, I advise you to already existing: http://docs.oracle.com/javase/7/docs/api/java/util/Set.html

The Set interface implements the basic set classes:

  • HashSet (based on the hash table)
  • LinkedHashSet (based on a linked list and a link hash table),
  • TreeSet (based on a balanced binary tree)

With a dynamic array and based on a simple linked list - the simplest implementations, but it is not used, because in sets, as a rule, indexing (an array) is not needed, indirect access and brute force (both in the array and in the list) are worth operations.

    Maybe it makes sense to specify an array of ints as a field (int [] set) and work with it:
    - add: you create a new array, put a new element in it and say that the set link now refers to this new extended array
    - remove: creates an array that is one less than the current one and all elements from the set that are! = element are copied into it. Then you just say that the set link now refers to this shortened array.

    • Thank you. And how, in this case, to create a constructor? - evgeniya
    • and what you need? - Stas0n
    • I understand you, thanks again. I am just starting to program and it seemed strange that the class has no attributes - evgeniya
    • Normally, the first steps are always the same .. I just didn’t understand something about the absence of attributes - are you talking about? - Stas0n
    • @ Stas0n apparently by attributes means fields - DreamChild