集合框架-ArrayList集合存储自定义对象_基本数据类型集合框架-ArrayList集合存储自定义对象_基本数据类型_02
 1 package cn.itcast.p3.arraylist.test;
 2 
 3 import java.util.ArrayList;
 4 import java.util.Iterator;
 5 
 6 import com.sun.org.apache.bcel.internal.generic.NEW;
 7 
 8 import cn.itcast.p.bean.Person;
 9 import sun.jvm.hotspot.ui.action.ShowAction;
10 
11 public class ArrayListTest {
12 
13     public static void main(String[] args) {
14         // TODO Auto-generated method stub
15         ArrayList al = new ArrayList();
16         al.add(new Person("list1",21));
17         al.add(new Person("list2",22));
18         al.add(new Person("list3",23));
19         al.add(new Person("list4",24));
20         
21         
22         Iterator it = al.iterator();
23         while (it.hasNext()) {
24             //System.out.println(((Person)it.next()).getName()+"::"+((Person) it.next()).getAge());
25             //list1::22
26             //list2::24 会多次调用it.next()会出错
27             Person person = (Person)it.next();
28             System.out.println(person.getName()+"::"+person.getAge());
29         }
30         
31         al.add(5);//al.add(new Integer(5)) ,jdk1.5以后可以直接用因为自动装箱
32         show(6);
33     }
34 
35     private static void show(Integer num) {//Object num = 6 //new Integer(6) 只要觉得类型符合就装箱,当基本数据类型给引用数据类型时就是装箱
36         // TODO Auto-generated method stub
37         int x = num + 8;//当引用数据类型和基本数据类型作运算 拆箱
38     }
39 
40 }
View Code
集合框架-ArrayList集合存储自定义对象_基本数据类型集合框架-ArrayList集合存储自定义对象_基本数据类型_02
 1 package cn.itcast.p.bean;
 2 
 3 public class Person {
 4     private String name;
 5     private int age;
 6     
 7     
 8     public Person() {
 9         super();
10         // TODO Auto-generated constructor stub
11     }
12     public Person(String name, int age) {
13         super();
14         this.name = name;
15         this.age = age;
16     }
17     public String getName() {
18         return name;
19     }
20     public void setName(String name) {
21         this.name = name;
22     }
23     public int getAge() {
24         return age;
25     }
26     public void setAge(int age) {
27         this.age = age;
28     }
29     //ctrl+alt+s 快捷构造set,get方法 初始化等
30 }
View Code