Java_基础—List集合存储学生对象并遍历

package com.soar.list;

import java.util.ArrayList;
import java.util.List;

import com.soar.bean.Student;

@SuppressWarnings({ "rawtypes", "unchecked" })
public class Demo2_List {
/*
 *  * A:案例演示
    * 向List集合中存储学生对象
    * 通过size()和get()方法结合使用遍历。
 */
    public static void main(String[] args) {
        List list = new ArrayList();
        list.add(new Student("张三",23)); //Object obj = new Student("张三",23);
        list.add(new Student("李四",24));
        list.add(new Student("王五",25));
        list.add(new Student("赵六",26));

        for (int i = 0; i < list.size(); i++) {
            //System.out.println(list.get(i));  //通过索引获取每一个元素
            Student s= (Student)list.get(i);
            System.out.println(s.getName()+"..."+s.getAge());
        }
    }

}

你可能感兴趣的:(Java_集合框架,java,对象,存储,遍历,class)