BlogAD

2014年12月10日 星期三

JAVA 使用iterator取出集合內所有的值

以下連結為範例來源出處(若有侵權 懇請告知)
http://pclevin.blogspot.tw/2011/12/java-iterator.html

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

public class test
{
    public static void main(String args[])
    {

        Set setTest = new HashSet();

        Iterator it;
        setTest.add("apple");
        setTest.add("banana");
        setTest.add("tomato");          

        it = setTest.iterator();

        while (it.hasNext())
        {
            System.out.println(it.next());
        }
    }
}

Java Iterator 範例

Java Iterator 範例

以下連結為範例來源出處(若有侵權 懇請告知)
http://www.dotspace.idv.tw/Jyemii/patternscolumn/articles/IteratorForJava.htm

不良的解決方式:
public class Person {
    private String name = "";
    public Person(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
}

public class ConcreteAggregate  {
    private Person[] namecollection;
    private int last = 0;
    public ConcreteAggregate(int maxsize) {
        this.namecollection = new Person[maxsize];
    }
    public Person getPersonAt(int index) {
        return namecollection[index];
    }
    public void appendPerson(Person personName) {
        this.namecollection[last] = personName;
        last++;
    }
    public int getLength() {
        return last;
    }
}

public class IteratorExample1 {
    public static void main(String[] args) {
        ConcreteAggregate namecollection = new ConcreteAggregate(3);
        Person person;
        namecollection.appendPerson(new Person("Davis"));
        namecollection.appendPerson(new Person("Frank"));
        namecollection.appendPerson(new Person("Jeny"));
        for(int i=0; i<namecollection.getLength(); i++)
         {
           person = namecollection.getPersonAt(i);
           System.out.println("" + person.getName());
         }
    }
}

利用 Iterator 模式來寫程式:
public interface Aggregate {
    public abstract Iterator iterator();
}

public interface Iterator {
    public abstract boolean hasNext();
    public abstract Object next();
}

import java.util.* ;
public class ConcreteAggregate implements Aggregate {
    private Object[] collection;
    //private Vector collection;
    private int last = 0;
    public ConcreteAggregate() {
        //collection = new Vector(3);
        collection = new Object[3];
     }

    public Object getItemAt(int index) {
        return collection[index];
       //return ((Object)collection.get(index));
    }

    public void appendItem(Object item) {
        this.collection[last] = item;
        last++;
        //collection.add(item);
    }

    public int getLength() {
        return last;
        //return collection.capacity();
    }

    public Iterator iterator() {
        return new ConcreteIterator(this);
    }
}

public class ConcreteIterator implements Iterator {
    private ConcreteAggregate namecollection;
    private int index;
    public ConcreteIterator(ConcreteAggregate collection) {
        this.namecollection = collection;
        this.index = 0;
    }
    public boolean hasNext() {
        if (index < namecollection.getLength()) {
            return true;
        } else {
            return false;
          }
    }
    public Object next() {
        Object item = namecollection.getItemAt(index);
        index++;
        return item;
    }
}

public class IteratorExample2 {
    public static void main(String[] args) {
        ConcreteAggregate collection = new ConcreteAggregate();
        collection.appendItem(new Person("Davis"));
        collection.appendItem(new Person("Frank"));
        collection.appendItem(new Person("Jeny"));
        Iterator it = collection.iterator();
        while (it.hasNext()) {
            Person person = (Person)it.next();
            System.out.println("" + person.getName());
        }
    }
}
這個程式碼應該符合 Iterator 設計模式的動機與目地,不管聚合物件的內部結構是什麼 ConcreteIterator 類別不須更改,仍可存取物件;利用遺傳方式可增加多種巡訪聚合物件的方式;聚合物件改變時(如 Array 改成 Vector)ConcreteIterator 類別仍可用相同的介面來巡訪聚合物件。
Java 已對 collections 實作了 Iterator 的功能  補充說明,則如下所示:
import java.util.* ;

public class IteratorExample3 {
    public static void main(String[] args) {
        Vector collection = new Vector(3); // 用 Arrays, List, ArrayList..均可
        collection.add(new Person("Davis"));
        collection.add(new Person("Frank"));
        collection.add(new Person("Jeny"));
        Iterator it = collection.iterator();
        while (it.hasNext()) {
            Person person = (Person)it.next();
            System.out.println("" + person.getName());
        }
    }
}