Created
November 4, 2011 21:11
-
-
Save lfborjas/1340495 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
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Iterator; | |
import java.util.NoSuchElementException; | |
class StringBag implements Iterable<String>, Iterator<String>{ | |
private ArrayList<String> bag; | |
private int current; | |
public StringBag(String... elements){ | |
bag = new ArrayList<String>(Arrays.asList(elements)); | |
current = 0; | |
} | |
public boolean hasNext(){ | |
return current <= bag.size()-1; | |
} | |
public String next() throws NoSuchElementException{ | |
if(current <= bag.size()-1){ | |
return bag.get(current++); | |
}else{ | |
throw new NoSuchElementException(); | |
} | |
} | |
public Iterator<String> iterator(){ | |
return this; | |
} | |
public void remove(){throw new UnsupportedOperationException("Me dio pereza programarlo");} | |
} | |
public class Iterators{ | |
public static void main (String [] args) | |
{ | |
StringBag b = new StringBag("hola", "mundo"); | |
for(String s : b) | |
System.out.println(s); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment