public static void main(String[] args) {
//demo1();
//int[] arr = new byte[5]; //数组要保证前后的数据类型一致
//ArrayList<Object> list = new ArrayList<Person>(); //集合的泛型要保证前后的数据类型一致
//ArrayList<Object> list = new ArrayList<>(); //1.7版本的新特性,菱形泛型,前面定义了,后面不加表示和前面一致
ArrayList<Object> list = new ArrayList<>(); //泛型最好不要定义成Object,没有意义
list.add("aaa");
list.add(true);
}
public static void demo2() {
ArrayList<Person> list = new ArrayList<Person>();
// list.add(110);
// list.add(true);
list.add(new Person("张三", 23));
list.add(new Person("李四", 24));
Iterator<Person> it = list.iterator();
while(it.hasNext()) {
//System.out.println(it.next());
//System.out.println(it.next().getName() + "..." + it.next().getAge());//next方法只能调用一次,如果调用多次会将指针向后移动多次
Person p = it.next();
System.out.println(p.getName() + "..." + p.getAge());
}
}
public static void demo1() {
//这里编译通过,运行包ClassCastException(类转换异常)
ArrayList list = new ArrayList();
list.add(110);
list.add(true);
list.add(new Person("张三", 23));
list.add(new Person("李四", 24));
Iterator it = list.iterator();
while(it.hasNext()) {
Person p = (Person)it.next();
System.out.println(p.getName() + "..." + p.getAge());
}
}
}作者: yuchifeifan 时间: 2016-2-20 01:40
妹子总结的好啊