我在执行引用Main中的ArrayList的System.out.print()
时遇到问题。我的代码......
import java.util.*;
public class Roster {
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@gmailcom", 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","Jack","Black","jblack14@wgu.edu", 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());}
}
}
public static void print_all(){
System.out.println("");
for (Student s: StudentArray) {
System.out.printf("%s\n",s);
}
}
//Print All Student Info
}
学生班
public class Student {
private String StuID;
private String FName;
private String LName;
private String Email;
private int Age;
private double Grade1;
private double Grade2;
private double Grade3;
public Student (String stuid, String fname, String lname, String email,
int age, double grade1, double grade2, double grade3)
{
this.StuID = stuid;
this.FName =fname;
this.LName = lname;
this.Email = email;
this.Age = age;
this.Grade1 = grade1;
this.Grade2 = grade2;
this.Grade3 = grade3;
}
public String getStuID(){
return this.StuID;
}
public String getFName(){
return this.FName;
}
public String getLName(){
return this.LName;
}
public String getEmail(){
return this.Email;
}
public int getAge(){
return this.Age;
}
public double getGrade1(){
return this.Grade1;
}
public double getGrade2(){
return this.Grade2;
}
public double getGrade3(){
return this.Grade3;
}
public String setStuID(String newStuID){
return (this.StuID= newStuID);
}
public String setFName(String newFName){
return (this.FName= newFName);
}
public String setLName(String newLName){
return (this.LName= newLName);
}
public String setEmail(String newEmail){
return (this.Email= newEmail);
}
public int setAge(int newAge){
return (this.Age= newAge);
}
public double setGrade1(double newGrade1){
return (this.Grade1= newGrade1);
}
public double setGrade2(double newGrade2){
return (this.Grade2= newGrade2);
}
public double setGrade3(double newGrade3){
return (this.Grade1= newGrade3);
}
public String toString() {
return String.format("StuID: %s\t First Name: %s\t Last Name: %s\t E-Mail: %s\t Age: %s\t Grade1: %s\t Grade2: %s\t Grade3: %s\t", this.StuID, this.FName, this.LName, this.Email,
this.Age, this.Grade1, this.Grade2, this.Grade3);
}
}
我知道这对某些人(或大多数人)来说可能是一项轻松的任务,但过去几天我一直在努力解决这个问题。如果我将“print_all”移动到Main(如“Example”)方法,它就可以正常工作。但是练习需要一个引用Main的新方法。如果你能帮到我,我将非常感激。我的大学的材料很难解释这一点。谢谢。
答案 0 :(得分:1)
也许你想做这样的事情:
import java.util.ArrayList;
public class Roster {
ArrayList<Student> studentArray;
public Roster(ArrayList<Student> ar)
{
studentArray=ar;
}
public void print_all(){
System.out.println("");{
for (Student s: studentArray) {
System.out.printf("%s\n",s);}}
//Print All Student Info
}
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@gmailcom", 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","Jack","Black","jblack14@wgu.edu", 65, 99, 98, 97));
//Example of printing specific student data using getters.
Roster r=new Roster(studentArray);
r.print_all();
}
}
正如您所看到的,方法print_all现在是类Roster的方法,您可以在构造函数中传递main中初始化的数组的引用。