Java 无法从另一个类调用ArrayList
我看了这里,有类似问题的答案,但我不能使它们符合我的代码。我的代码: 学生班 我遇到的问题是,我试图在Java 无法从另一个类调用ArrayList,java,arraylist,Java,Arraylist,我看了这里,有类似问题的答案,但我不能使它们符合我的代码。我的代码: 学生班 我遇到的问题是,我试图在花名册类中执行系统.out.print。我得到的错误是花名册类中的“StudentArray无法解析为变量”。如果我在StudentArrayList中的main方法下执行此操作,则打印方法工作正常。我知道我必须有办法调用数组,只是不知道该怎么做。那会是一个成功者吗 “无法将StudentArray解析为变量” 这意味着您尚未声明变量StudentArray public class Roste
花名册
类中执行系统.out.print
。我得到的错误是花名册
类中的“StudentArray无法解析为变量”。如果我在StudentArrayList
中的main
方法下执行此操作,则打印方法工作正常。我知道我必须有办法调用数组,只是不知道该怎么做。那会是一个成功者吗
“无法将StudentArray解析为变量”
这意味着您尚未声明变量StudentArray
public class Roster {
public static void main(String[]args){
ArrayList<Student> StudentArray = new ArrayList<Student>();
for (Student s: StudentArray) { // loop through the array
System.out.printf("%s\n",s);
}
}
}
公共课堂花名册{
公共静态void main(字符串[]args){
ArrayList StudentArray=新的ArrayList();
对于(Student s:StudentArray){//循环遍历数组
System.out.printf(“%s\n”,s);
}
}
}
您需要首先声明studentArray
ArrayList<Student> studentArray = new ArrayList<Student>();
然后打印整个列表:
for(Student s: studentArray){
System.out.println(%s\n",s);
}
我认为你根本不需要花名册,因为你想让程序在学生课堂上完成。您不能有两个具有main的类,学生类和花名册类都具有main 学生课堂上的主要任务是做正确的事情,初始化学生并打印花名册 因此,您可以做两件事删除花名册类或删除此代码
public class StudentArrayList {
public static void main(String[]args){
ArrayList<Student> StudentArray = new ArrayList<Student>();
StudentArray.add(new Student("1","John","Smith","John1989@gmail.com", 20, 88, 79, 59));
StudentArray.add(new Student("2","Susan","Erickson","Erickson_1990@gmail.com", 19, 91, 72, 85));
StudentArray.add(new Student("3","Jack","Napoli","The_lawyer99yahoo.com", 19, 85, 84, 87));
StudentArray.add(new Student("4","Erin","Black","Erin.black@comcast.net", 22, 91, 98, 82));
StudentArray.add(new Student("5","Tom","Gaye","tgay@att.net", 65, 99, 98, 97));
//Example of printing specific student data using getters.
System.out.println("");
for (Student a: StudentArray) {
System.out.println(a.getStuID());
System.out.println(a.getFName());
System.out.println(a.getLName());
}
}
公共类StudentArrayList{
公共静态void main(字符串[]args){
ArrayList StudentArray=新的ArrayList();
添加(新学生(“1”、“约翰”、“史密斯”)、“John1989@gmail.com", 20, 88, 79, 59));
添加(新学生(“2”、“Susan”、“Erickson”、“Erickson”)_1990@gmail.com", 19, 91, 72, 85));
添加(新学生(“3”,“杰克”,“那不勒斯”,“The_lawyer99yahoo.com”,19,85,84,87));
学生阵列。添加(新学生(“4”、“艾琳”、“黑色”、“艾琳”)。black@comcast.net", 22, 91, 98, 82));
添加(新学生(“5”、“汤姆”、“盖伊”)tgay@att.net", 65, 99, 98, 97));
//使用getter打印特定学生数据的示例。
System.out.println(“”);
对于(学生a:学生阵列){
System.out.println(a.getStuID());
System.out.println(a.getFName());
System.out.println(a.getLName());
}
}
对花名册来说,这就足够了
Student stud1 = new Student("stud params");
//other students declaration
//you could do this with a loop if data are asked to user
studentArray.add(stud1);
for(Student s: studentArray){
System.out.println(%s\n",s);
}
public class StudentArrayList {
public static void main(String[]args){
ArrayList<Student> StudentArray = new ArrayList<Student>();
StudentArray.add(new Student("1","John","Smith","John1989@gmail.com", 20, 88, 79, 59));
StudentArray.add(new Student("2","Susan","Erickson","Erickson_1990@gmail.com", 19, 91, 72, 85));
StudentArray.add(new Student("3","Jack","Napoli","The_lawyer99yahoo.com", 19, 85, 84, 87));
StudentArray.add(new Student("4","Erin","Black","Erin.black@comcast.net", 22, 91, 98, 82));
StudentArray.add(new Student("5","Tom","Gaye","tgay@att.net", 65, 99, 98, 97));
//Example of printing specific student data using getters.
System.out.println("");
for (Student a: StudentArray) {
System.out.println(a.getStuID());
System.out.println(a.getFName());
System.out.println(a.getLName());
}
}