本帖最后由 holistic杀手 于 2022-12-10 18:35 编辑 import java.util.ArrayList;
import java.util.Scanner;
//测试学生类
public class StudentTest {
public static void main(String[] args) {
ArrayList list = new ArrayList();
System.out.println("添加学生");
Student.addStudent(list);
Student.addStudent(list);
Student.addStudent(list);
for (int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
}
}
//学生类
class Student {
String name;
String uid;
public Student(String name,String uid){
this.name= name;
this.uid=uid;
}
public static void addStudent(ArrayList list){
Scanner s = new Scanner(System.in);
System.out.print("输入学号:");
String uid = s.next();
System.out.print("输入姓名:");
String name= s.next();
Student stu = new Student(name,uid);
list.add(stu);
System.out.println("添加成功");
}
@Override
public String toString() {
return this.name+","+this.uid;
}
}
我输入的学生信息添加到了列表里面,遍历出来了。 |