Foros aprenderaprogramar.com
Aprender a programar => Aprender a programar desde cero => Mensaje iniciado por: Falricas en 07 de Diciembre 2017, 04:28
-
Buenas noches acá esta mi solución al tema de polimorfismo CU00678B del tutorial pdf de programación Java. Me compliqué un poco al hacerlo con iteradores, utilice la interface Serializable que implementan las clases String e Integer, Gracias.
import java.util.*;
import java.io.Serializable;
public class TestSet{
public static void main(String[] args){
ArrayList<Set> miArray1 = new ArrayList<Set>();
HashSet<String> hashSetObj = new HashSet<String>();
TreeSet<Integer> treeSetObj = new TreeSet<Integer>();
hashSetObj.add("sol");
hashSetObj.add("luna");
hashSetObj.add("saturno");
treeSetObj.add(8);
treeSetObj.add(5);
treeSetObj.add(2);
miArray1.add(hashSetObj);
miArray1.add(treeSetObj);
Iterator<Set> it1 = miArray1.iterator();
while(it1.hasNext()){
Iterator<Serializable> it2 = it1.next().iterator();
while(it2.hasNext())
System.out.print(it2.next() + " ");
}
}
}
-
Hola, el ejercicio hace lo que se pedía pero al utilizar la interface Serializable se sale del programa del curso por dos motivos. Uno por usar una interface, algo que no se ha explicado aún en esta entrega del curso. Y otra por recurrir a algo que no se ha usado durante las explicaciones del curso. No obstante, hay algo positivo, que es el "afán de investigación". En este hilo hay una solución adaptada al programa del curso: https://www.aprenderaprogramar.com/foros/index.php?topic=2384.0
Saludos.