package lijun.cn.demo1; import java.util.ArrayList; import java.util.Random; public class CallName { public static void main(String[] args) { ArrayList<Student> arr=new ArrayList<Student>();
//添加 add(arr);
//遍历 printArrayList(arr);
//随机 randomStudentName(arr); } private static void randomStudentName(ArrayList<Student> arr) { Random r=new Random(); int num=r.nextInt(arr.size()); Student s=arr.get(num); System.out.println("============================="); System.out.println(s.name+" "+s.age); } private static void printArrayList(ArrayList<Student> arr) { for(int i=0;i<arr.size();i++){ Student s=arr.get(i); System.out.println(s.name+" "+s.age); } } private static void add(ArrayList<Student> arr) { Student s1=new Student(); s1.name="张无忌"; s1.age=22; Student s2=new Student(); s2.name="无双"; s2.age=21; Student s3=new Student(); s3.name="建宁"; s3.age=20; Student s4=new Student(); s4.name="龙儿"; s4.age=25; arr.add(s1); arr.add(s2); arr.add(s3); arr.add(s4); } }