使用传统方法的问题分析
不能对加入到集合ArrayList中的数据类型进行约束(不安全)
遍历的时候,需要进行类型转换,如果集合中的数据量较大,对效率有影响
//传统方式package 泛型;import org.w3c.dom.DOMErrorHandler;import java.util.ArrayList;import java.util.Map;@SuppressWarnings({"All"})public class Demo01 { public static void main(String[] args) { ArrayList list = new ArrayList(); list.add(new Dog("小白",1)); list.add(new Dog("小黄",2)); list.add(new Dog("发财",3)); list.add(new Cat("来福",1)); for (Object o :list) { Dog dog = (Dog)o; String name = dog.getName(); int age = dog.getAge(); System.out.println(name+"-"+age+"岁"); } }}class Cat{ private String name; private int age; public Cat(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; }}class Dog{ private String name; private int age; public Dog(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; }