java--exceptions

/*Explicit Exceptions:
 * We can also throw our own exceptions using the throw keyword.
 *1) Can provide more information message to a user.
 *2) Can provide more information to code that "catches " the exceptions.
 */



/*The Enhanced For Loop 
 * Java allows us to iterate through Lists and Sets using 
 * a convenient shorthand syntax sometimes called the
 * "foreeach" or  "enhanced for" loop.
 */



/*For-each Iteration And ArraySets
 *To support theenchanced for loop,
 we need to make ArraySet implements. 
 *There are also some default methods in Iterable, not shown.
 *
 * public interfce Iterator<T> {
 *    Iterator<T> iterator();
 *
 * }
 *
 */

import java.util.HashSet;
import java.util.Set;
import java.util.Iterator;


public class ArraySet<T> implements Iterable<T>{
    private T[] items;
    private int size;
    
    public ArraySet() {
        items = (T[]) new Object[100];
        size = 0;
    }
    
    /*Returns true if this map contains a mapping for the specified key*/

    public boolean contains(T x) {

        for(int i = 0; i < size; i+= 1) {
            if(items[i] == null) {
                if(x == null) {
                    return true;
                }
            }
            if(x.equals(items[i])) {
                return true;
            }
        }
        return false;
    }
    
    
    public void add(T x) {
        if(x == null) {
            throw new IllegalArgumentException("You can't add null to an ArraySet");
        }
        if(contains(x)) {
            return;
        }
        items[size] = x;
        size += 1;
    }
    
    public int size() {
        return size;
    }
    
    /**returns an iterator into ME. */
    public Iterator<T> iterator() {
        return new ArraySetIterator();
    }
    
    /*public interface Iterator <T> {
     *        boolean hasNext();
     *        T next();
     * }
     */
    
    private class ArraySetIterator implements Iterator<T> {
        private int wizPos;
        public ArraySetIterator() {
            wizPos = 0;
        }

        public boolean hasNext() {
            return wizPos < size;
        }

        public T next() {
            T returnItem = items[wizPos];
            wizPos += 1;
            return returnItem;
        }
    }

    /*The toString() method provides a string representation
     * of an object. 
     *
     *System.out.println(Object x) calls x.toString();
    The default toString() is the name of the class, @,memory location 
    of the object;

     */

    @Override
    public String toString() {
//        String returnString = "{";
        StringBuilder returnSB = new StringBuilder("{");    
            for(int i = 0; i < size-1; i+=1) {
                returnSB.append(items[i].toString());
                returnSB.append( ", ");
            }
            returnSB.append(items[size-1]);
            returnSB.append( "}");
            return returnSB.toString();


    /*    for(T item : this) {
            returnString += item.toString();
            returnString += ",";
        }*/
    }

    /* Equals vs. == 
     * == compares the bits. For reference, == means "referencing the same object"
     *
     */
    @Override 
    public boolean equals(Object other){
        if(this == other) {return true; }
        if(other == null) { return false; }
        if (other.getClass() != this.getClass()) {
            return false;
        }
        ArraySet<T> o = (ArraySet<T>) other;
        if (o.size() != this.size()) {
            return false;
        } 
        for(T item : this) {
            if(!o.contains(item)) {
                return false;
            }
        }
        return true;
    }
    

    public static void main(String[] args) {
        /*Set<Integer> javaset = new HashSet<>();
        javaset.add(5);
        javaset.add(23);
        javaset.add(42);
        
        Iterator<Integer> seer = javaset.iterator();
        
        while(seer.hasNext()) { 
            int i = seer.next();
            System.out.println(i);
        }*/

        ArraySet<Integer> aset = new ArraySet<>();
        aset.add(5);
        aset.add(23);
        aset.add(42);
        System.out.println(aset);
        Iterator<Integer> aseer = aset.iterator();
/*        while(aseer.hasNext()) { 
            int i = aseer.next();
            System.out.println(i);
        }
        
        for(int i : aset){
            System.out.println(i);
        }*/


        System.out.println(aset.equals(null));
        System.out.println(aset.equals("fish") );
        System.out.println(aset.equals(aset));
/*        ArraySet<String> s = new ArraySet<>();
        s.add(null);
        s.add("horse");
        s.add("fish");
        s.add("house");
        s.add("fish");
        System.out.println(s.contains("horse"));
        System.out.println(s.size());*/
        
    }

}

 

posted @ 2019-03-03 14:42  卷积  阅读(447)  评论(0编辑  收藏  举报