Array Set : Array Collections « Collections Data Structure « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Collections Data Structure » Array CollectionsScreenshots 
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[] argsthrows 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 == nullbreak;
            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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.