Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ed_ficha4;
- import java.util.Iterator;
- import java.util.NoSuchElementException;
- public class DezReais implements Iterable<Double> {
- private final int TAMANHO = 10;
- private Double []tab = new Double[TAMANHO];
- private int last = 0;
- public boolean add(Double d){
- if(size() >= TAMANHO)throw new RuntimeException();
- tab[last++] = d;
- return true;
- }
- public int size(){
- return last;
- }
- public Double get (int pos){
- return tab[pos];
- }
- @Override
- public Iterator<Double> iterator() {
- return new ItDezReais(this) {};
- }
- }
- class ItDezReais implements Iterator<Double>{
- DezReais lista;
- int contador = -1;
- ItDezReais (DezReais d){
- lista = d;
- }
- @Override
- public boolean hasNext() {
- return contador+1 < lista.size();
- }
- @Override
- public Double next() {
- if(contador < 10)
- return lista.get(++contador);
- else
- throw new NoSuchElementException();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement