daily pastebin goal
92%
SHARE
TWEET

Untitled

a guest Mar 19th, 2018 175 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class MyIntSet implements IntSet{
  2.     private int size;
  3.     private int elem[];
  4.  
  5.     MyIntSet(){
  6.         elem = new int[100];
  7.         size = 0;
  8.     }
  9.  
  10.     public boolean contains(int x){
  11.         for(int i=0; i<size; i++)
  12.             if(elem[i] == x) return true;
  13.         return false;
  14.     }//contains
  15.  
  16.     public boolean add(int x){
  17.         if(!contains(x)){
  18.            if(size == elem.length) throw new RuntimeException("Maximum size of set reached");  
  19.            elem[size] = x;
  20.            size++;
  21.            return true;
  22.         }
  23.         return false;
  24.     }//add
  25.  
  26.     public boolean remove(int x){
  27.         if(contains(x)){
  28.            int pos = 0;
  29.            while(elem[pos] != x) pos++;
  30.            size--;
  31.            elem[pos] = elem[size];
  32.            return true;            
  33.         }
  34.         return false;
  35.    }//remove
  36.  
  37.     public int size(){
  38.         return size;
  39.    }
  40.  
  41.     public void clear(){
  42.         size = 0;
  43.     }
  44.     /*
  45.     public String toString(){
  46.       String res = "{";
  47.       for (int i=0; i<size; i++) {
  48.          if (i>0) res += ",";
  49.          res += elem[i];
  50.       }
  51.       res += "}";
  52.       return res;
  53.     }
  54.     */
  55.  
  56. }//MyIntSet
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top