Код: Выделить всё
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
abstract class Chordate {}
abstract class Mammal extends Chordate {}
abstract class Insectivore extends Mammal {}
abstract class Predator extends Mammal {}
abstract class Erinaceidae extends Insectivore {}
abstract class Felidae extends Predator {}
class Hedgehog extends Erinaceidae {}
class PallasCat extends Felidae {}
class Lynx extends Felidae {}
public class AnimalSegregator {
public static void segregate(
Collection srcCollection,
Collection collection) {
System.out.println("\nКоллекция " + name + " (" + collection.size() + " элементов):");
if (collection.isEmpty()) {
System.out.println(" [пусто]");
return;
}
for (Object item : collection) {
System.out.println(" - " + item + " (класс: " + item.getClass().getSimpleName() + ")");
}
}
public static void main(String[] args) {
List animals1 = new ArrayList();
animals1.add(new Hedgehog());
animals1.add(new PallasCat());
animals1.add(new Lynx());
animals1.add(new Hedgehog());
animals1.add(new PallasCat());
List hedgehogs = new ArrayList();
List
pallasCats = new ArrayList();
List lynxes = new ArrayList();
AnimalSegregator.segregate(animals1, hedgehogs, pallasCats, lynxes);
System.out.println("=== Пример 1 ===");
System.out.println("Ежи: " + hedgehogs.size() + " (" + hedgehogs + ")");
System.out.println("Манулы: " + pallasCats.size() + " (" + pallasCats + ")");
System.out.println("Рыси: " + lynxes.size() + " (" + lynxes + ")");
}
}
Подробнее здесь: https://stackoverflow.com/questions/795 ... rms-to-hed