Created
June 21, 2014 03:23
-
-
Save gary-liguoliang/87ab06181ad773c1f9da to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.liguoliang.lang.collection; | |
import java.util.ArrayList; | |
import java.util.Iterator; | |
import java.util.List; | |
public class GList <T> implements Iterable<T>{ | |
private List<T> _items = new ArrayList<T>(); | |
public GList<T> add(T ... ts) { | |
for (T t : ts) { | |
_items.add(t); | |
} | |
return this; | |
} | |
public int getSize() { | |
return _items.size(); | |
} | |
@Override | |
public String toString() { | |
return "GList [_items=" + _items + "]"; | |
} | |
@Override | |
public Iterator<T> iterator() { | |
return new Itr(); | |
} | |
private class Itr implements Iterator<T> { | |
int cursor = -1; | |
@Override | |
public boolean hasNext() { | |
return cursor < _items.size(); | |
} | |
@Override | |
public T next() { | |
if(cursor < 0) { | |
cursor = 0; | |
} | |
if(cursor > _items.size() - 1) { | |
throw new IndexOutOfBoundsException(); | |
} | |
T item = null; | |
item = _items.get(cursor); | |
cursor++; | |
return item; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment