学生点名器
package g666.example2; import java.util.ArrayList; import java.util.Random; public class callNameDemo { public static void main(String[] args) { //创建一个集合,用来存储 ArrayList<Stdent> array=new ArrayList<Stdent>(); addStudnet(array); look(array); randomStudentname(array); } //添加学生到集合里面 public static void addStudnet(ArrayList<Stdent> array) { Stdent s1=new Stdent(); s1.name="张三"; s1.age=22; Stdent s2=new Stdent(); s2.name="李四"; s2.age=23; Stdent s3= new Stdent(); s3.name="王五";s3.age=24; } //查看学生 public static void look(ArrayList<Stdent> array) { for(int a =0; a<array.size(); a++) { System.out.println(array.get(a).name+" "+array.get(a).age); } } //随机查看 public static void randomStudentname(ArrayList<Stdent> array) { int random=new Random().nextInt(array.size()); System.out.println(array.get(random).name); } } class Stdent { int age; String name; }