【发布时间】:2020-02-10 01:00:47
【问题描述】:
public class pro1{
static ArrayList <String> student = new ArrayList<String>();
static ArrayList <Integer> id = new ArrayList<Integer>();
String name;
int ID;
public pro1() {
this.name = "";
this.ID = 0;
}
public pro1(String name, int ID) {
this.name = name;
this.ID = ID;
}
public boolean addStudent(String name, int ID) {
student.add(name);
id.add(ID);
return true;
}
/*@Override
public String toString() {
return name + ID;
}*/
public static void main(String args[]) {
pro1 stu = new pro1();
stu.addStudent("john", 1);
stu.addStudent("johnny", 2);
System.out.println(stu);
}
}
我想使用 ArrayList 打印出学生的姓名和学生 ID。但是,我不知道该怎么做,因为在这个类中我只能打印出名称的 ArrayList 或 id 的 ArrayList。我正在考虑使用另一个类来创建一个学生对象,但我不知道该怎么做。
【问题讨论】:
-
从学生列表中分离学生对象。你应该有一个包含
id和name的学生类,然后你可以有一个学生列表。