package com.sise05; import java.util.ArrayList; import java.util.Scanner; public class Test2 { public static void main(String[] args) { ArrayList<Student> arrayList=new ArrayList<>(); ArrayAdd(arrayList); ArrayAdd(arrayList); ArrayAdd(arrayList); for (int i=0;i<arrayList.size();i++){ Student s=arrayList.get(i); System.out.println("stu"+i+":"+s.getName()+","+s.getAge()); } } public static void ArrayAdd(ArrayList<Student> arrayList){ Scanner sc=new Scanner(System.in); Student stu=new Student(); System.out.println("请输入学生名字:"); String name=sc.nextLine(); System.out.println("请输入学生年龄:"); int age=sc.nextInt(); stu.setName(name); stu.setAge(age); arrayList.add(stu); } }
ArrayList调用方法存储学生对象并遍历
最新推荐文章于 2022-03-27 20:35:12 发布