Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class IntSet {
- //OVERVIEW: IntSets are unbounded, mutable sets of integers.
- private Vector els;
- // the rep
- // constructors
- public IntSet(){
- //EFFECTS: Initializes this to be empty.
- els = new Vector( );
- }
- // methods
- public void insert (int x) {
- //MODIFIES: this
- //EFFECTS: Adds x to the elements of this.
- Integer y = new Integer(x);
- if (getIndex(y) < 0) els.add(y);
- }
- public void remove (int x) {
- //MODIFIES: this
- //EFFECTS: Removes x from this.
- int i = getIndex(new Integer(x));
- if (i < 0) return;
- els.set(i, els.lastElement( ));
- els.remove(els.size( ) - 1);
- }
- public boolean isIn (int x) {
- //EFFECTS: Returns true if x is in this else returns false.
- return getIndex(new Integer(x)) >= 0;
- }
- private int getIndex (Integer x) {
- //EFFECTS: If x is in this returns index where x appears else returns -1.
- for (inti=0;i< els.size( ); i++)
- if (x.equals(els.get(i))) return i;
- return -1;
- }
- public int size( ){
- //EFFECTS: Returns the cardinality of this.
- return els.size( );
- }
- public int choose ( ) throws EmptyException {
- //EFFECTS: If this is empty throws EmptyException else
- //returns an arbitrary element of this.
- if (els.size( ) == 0) throw new EmptyException("IntSet.choose");
- return els.lastElement( );
- }
- }
Add Comment
Please, Sign In to add comment