/ Published in: Java
Funky
Expand |
Embed | Plain Text
import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.Random; /** * * @author wuaddiso */ public class FunkyList<K> implements Iterable<K> { ArrayList<K> array = new ArrayList<K>(); public void add(K item){ array.add(item); } public K remove(){ return array.remove(0); } public Iterator<K> iterator() { return new FunkyIterator(); } private class FunkyIterator implements Iterator<K>{ ArrayList<Integer> history = new ArrayList<Integer>(); public boolean hasNext() { return (history.size()<array.size()); } public K next() { if(history.size()==array.size()) //if all indices have been used history.clear(); int index = generator.nextInt(array.size()); while(history.contains(index)){ //if (index has been used before) //generate new index index = generator.nextInt(array.size()); } history.add(index); return array.get(index); } public void remove() { } } }
You need to login to post a comment.
