public static void main(String args[]) { // creamos una lista para guardar cuentas de Inversion SimpleList c = new SimpleList(); c.addInOrder(new Inversion(101, 2000, 2.1f)); c.addInOrder(new Inversion(212, 1000, 1.2f)); c.addInOrder(new Inversion(511, 3000, 3)); System.out.println("\nLista de Cuentas: " + c); // ahora recorremos la lista para calcular el saldo promedio... // ...usamos nuestra implementación liviana del patrón Iterator float a = 0; int b = 0; c.startIterator(); // inicializamos el mecanismo de recorrido while (c.hasNext()) { Inversion x = (Inversion) c.next(); a += x.getSaldo(); b++; } float prom = 0; if (b != 0) prom = a / b; System.out.println("Saldo Promedio: " + prom); // para recorrer la lista otra vez, reinicializamos el mecanismo iterador... c.startIterator(); int s = 0; while (c.hasNext()) { Inversion y = (Inversion) c.next(); if (y.getSaldo() > prom) s++; } System.out.println("Cantidad de cuentas con saldo mayor al promedio: " + s); }
@Override public List<T> toJDKList() { List<T> result = new ArrayList<T>(); result.addAll(list.toJDKList()); result.add(lastElement); return result; }
/** * Returns the next element in the iteration. * * @return the next element in the iteration. */ public Object next() { if (!hasNext()) { throw new ArrayIndexOutOfBoundsException("Iterator out of Bounds"); } else { return list.get(--current); } }
@Override public T get(int index) { if (index < list.size()) return list.get(index); if (index == list.size()) return lastElement; throw new IndexOutOfBoundsException("No such element:" + index); }
public OneMoreElementList(SimpleList<T> list, T extraElement) { this.list = list; this.lastElement = extraElement; this.size = list.size() + 1; }
/** * Creates a new ReverseIterator from the given list. * * @param list the list to generate an iterator from */ public ReverseIterator(SimpleList list) { super(); this.list = list; current = list.count(); }