
1 package com.mon11.day5; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 import java.util.Scanner; 6 7 /** 8 * 类说明 : 9 * @author 作者 : chenyanlong 10 * @version 创建时间:2017年11月5日 11 */ 12 //定义学生类 13 class Student{ 14 String name; 15 int age; 16 public String getName() { 17 return name; 18 } 19 public int getAge() { 20 return age; 21 } 22 23 public Student(String name,int age){ 24 this.name=name; 25 this.age=age; 26 } 27 28 } 29 30 public class ListDemo2 { 31 32 //定义成员变量集合 33 static List list=new ArrayList(); 34 35 //main主方法 36 public static void main(String[] args) { 37 38 39 ListDemo2 lisDemo2=new ListDemo2(); 40 lisDemo2.input(); 41 lisDemo2.output(); 42 43 } 44 45 //学生信息输出 46 public void output(){ 47 System.out.println("总共有多少个学生"+list.size()+"个"); 48 //通过遍历输出所有学生的学习 49 for(int i=0;i<list.size();i++){ 50 Student student=(Student) list.get(i); 51 System.out.println(student.getName()+" "+student.age); 52 } 53 } 54 55 //学生信息输入 56 public void input(){ 57 Student stu1=new Student("tom1",11); 58 Student stu2=new Student("tom2",12); 59 Student stu3=new Student("tom3",13); 60 Student stu4=new Student("tom4",14); 61 list.add(stu1); 62 list.add(stu2); 63 list.add(stu3); 64 list.add(stu4); 65 } 66 67 }