Implementa la siguiente función, utilizando un objeto de la clase Stack.
- Nombre y parámetros: List<Point> invertir (Iterator<Point> i , Point p)
- Retorna: List<Point> (realmente será un objeto ArrayList<Point>)
- Tarea: Crea un ArrayList de objetos Point que contenga los puntos obtenidos con el iterador pero en orden inverso, excluyendo los puntos que sean iguales al recibido en el segundo parámetro.
- Si el segundo parámetro es null, se excluyen los puntos (0,0)
- Excepciones: IllegalArgumentException en caso de que el iterator no nos diera ni un solo Punto o bien fuese null.
Se añade el programa principal con el que se probó la función.
public class EjercicioStack {
public static void main (String[] args) {
Point a = new Point(0,0);
Point b = new Point(1,1);
Point c = new Point(2,2);
Point d = null;
ArrayList<Point> puntos = new ArrayList<Point>();
ArrayList<Point> puntosInv;
puntos.add(a);
puntos.add(b);
puntos.add(c);
System.out.println(puntos);
Iterator<Point> it = puntos.iterator();
puntosInv = (ArrayList<Point>) invertir(it, d);
System.out.println(puntosInv);
}
public static List<Point> invertir (Iterator<Point> i, Point p) {
Stack<Point> pila = new Stack<Point>();
ArrayList<Point> puntos = new ArrayList<Point>();
Point aux;
if (i == null || !i.hasNext()) {
throw new IllegalArgumentException();
}
if (p == null) {
p = new Point(0, 0);
}
while (i.hasNext()) {
aux = i.next();
if (!aux.equals(p))
pila.push(aux);
}
while (!pila.empty()) {
puntos.add(pila.pop());
}
return puntos;
}
}
No hay comentarios:
Publicar un comentario