Array Set : Array Collections « Collections Data Structure « Java






Array Set

Array Set
   
/* Copyright (c) 2006, 2009, Carl Burch. License information is located in the
 * com.cburch.logisim.Main source code and at www.cburch.com/logisim/. */
 

import java.util.AbstractSet;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.NoSuchElementException;

public class ArraySet extends AbstractSet {
    private static final Object[] EMPTY_ARRAY = new Object[0];
    
    private class ArrayIterator implements Iterator {
        int itVersion = version;
        int pos = 0; // position of next item to return
        boolean hasNext = values.length > 0;
        boolean removeOk = false;
        
        public boolean hasNext() {
            return hasNext;
        }
        
        public Object next() {
            if(itVersion != version) {
                throw new ConcurrentModificationException();
            } else if(!hasNext) {
                throw new NoSuchElementException();
            } else {
                Object ret = values[pos];
                ++pos;
                hasNext = pos < values.length;
                removeOk = true;
                return ret;
            }
        }
        
        public void remove() {
            if(itVersion != version) {
                throw new ConcurrentModificationException();
            } else if(!removeOk) {
                throw new IllegalStateException();
            } else if(values.length == 1) {
                values = EMPTY_ARRAY;
                ++version;
                itVersion = version;
                removeOk = false;
            } else {
                Object[] newValues = new Object[values.length - 1];
                if(pos > 1) {
                    System.arraycopy(values, 0, newValues, 0, pos - 1);
                }
                if(pos < values.length) {
                    System.arraycopy(values, pos, newValues, pos - 1, values.length - pos);
                }
                values = newValues;
                --pos;
                ++version;
                itVersion = version;
                removeOk = false;
            }
        }
    }
    
    private int version = 0;
    private Object[] values = EMPTY_ARRAY;
    
    public ArraySet() { }
    
    public Object[] toArray() {
        return values;
    }
    
    public Object clone() {
        ArraySet ret = new ArraySet();
        if(this.values == EMPTY_ARRAY) {
            ret.values = EMPTY_ARRAY;
        } else {
            ret.values = (Object[]) this.values.clone();
        }
        return ret;
    }
    
    public void clear() {
        values = EMPTY_ARRAY;
        ++version;
    }
    
    public boolean isEmpty() {
        return values.length == 0;
    }

    public int size() {
        return values.length;
    }
    
    public boolean add(Object value) {
        int n = values.length;
        for(int i = 0; i < n; i++) {
            if(values[i].equals(value)) return false;
        }
        
        Object[] newValues = new Object[n + 1];
        System.arraycopy(values, 0, newValues, 0, n);
        newValues[n] = value;
        values = newValues;
        ++version;
        return true;
    }
    
    public boolean contains(Object value) {
        for(int i = 0, n = values.length; i < n; i++) {
            if(values[i].equals(value)) return true;
        }
        return false;
    }

    public Iterator iterator() {
        return new ArrayIterator();
    }
    
    public static void main(String[] args) throws java.io.IOException {
        ArraySet set = new ArraySet();
        java.io.BufferedReader in = new java.io.BufferedReader(
                new java.io.InputStreamReader(System.in));
        while(true) {
            System.out.print(set.size() + ":"); //OK
            for(Iterator it = set.iterator(); it.hasNext(); ) {
                System.out.print(" " + it.next()); //OK
            }
            System.out.println(); //OK
            System.out.print("> "); //OK
            String cmd = in.readLine();
            if(cmd == null) break;
            cmd = cmd.trim();
            if(cmd.equals("")) {
                ;
            } else if(cmd.startsWith("+")) {
                set.add(cmd.substring(1));
            } else if(cmd.startsWith("-")) {
                set.remove(cmd.substring(1));
            } else if(cmd.startsWith("?")) {
                boolean ret = set.contains(cmd.substring(1));
                System.out.println("  " + ret); //OK
            } else {
                System.out.println("unrecognized command"); //OK
            }
        }
    }
}

   
    
    
  








Related examples in the same category

1.Array Iterator
2.Array MapArray Map
3.Array Int Set
4.Remove duplicate element from array
5.Convert an Array to a List
6.Converting an Array to a Collection
7.Converting a Collection of user objects to an Array
8.Create an array containing the elements in a set
9.Convert an array to a Map
10.Converting a Collection of String to an ArrayConverting a Collection of String to an Array
11.Treating an Array as an Enumeration
12.ArrayEnumeration class (implements Enumeration)ArrayEnumeration class (implements Enumeration)
13.Custom ArrayMap implementation (extends AbstractMap)Custom ArrayMap implementation (extends AbstractMap)
14.Custom ArraySet implementation (extends AbstractSet)Custom ArraySet implementation (extends AbstractSet)
15.Converts array into a java.util.Map.
16.Growable array of intsGrowable array of ints
17.Growable array of floats.
18.Acts like an java.util.ArrayList but for primitive int valuesActs like an java.util.ArrayList but for primitive int values
19.Acts like an java.util.ArrayList but for primitive long values
20.Add array to collection