简体   繁体   English

坚持从另一个类调用ArrayList

[英]Stuck on calling on an ArrayList from another class

I have looked here and there are answers to similar questions, but I cannot conform them to my code. 我看过这里有类似问题的答案,但我无法将它们与我的代码相符。 My code: 我的代码:

Student Class/StudentArrayList Class Student Class / StudentArrayList类

import java.util.*;

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 EMail: %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);

    }
}

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());

        }
    }
}

Roster Class 名册班

public class Roster {
    public static void main(String[]args){
        System.out.println("");
        for (Student s: StudentArray) { //THIS IS WHERE IT ERRORS
            System.out.printf("%s\n",s);
        }
    }
}

The problem I am having is I am trying to perform a System.out.print in the Roster class. 我遇到的问题是我正在尝试在Roster类中执行System.out.print The error I am getting is "StudentArray cannot be resolved to a variable" in the Roster class. 我得到的错误是“ Roster类中的StudentArray无法解析为变量”。 If I do this under the main method in the StudentArrayList , the print method works just fine. 如果我在StudentArrayListmain方法下执行此操作,则print方法可以正常工作。 I know I have to have a way to call the array, just unsure what to do. 我知道我必须有办法调用阵列,只是不确定该怎么做。 Would that be a getter? 那会是一个吸气剂吗?

"StudentArray cannot be resolved to a variable" “StudentArray无法解析为变量”

That means that you haven't declared the variable 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);
        }
    }
}

You need to first declare the studentArray 您需要先声明studentArray

ArrayList<Student> studentArray = new ArrayList<Student>();

populate it adding some students, for example: 填充它添加一些学生,例如:

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);

and then print the whole list: 然后打印整个列表:

for(Student s: studentArray){
     System.out.println(%s\n",s);
}

I don't think you need the Roster class at all, coz what you want the program to do is being done in the Student class itself. 我认为你根本不需要Roster课程,因为你想要的程序是在Student课程中完成的。 You cannot have two classes with main , both the Student Class and the Roster Class has Main. 你不能有两个带有main的类,Student Class和Roster Class都有Main。

The Main in student class is doing the right thing , it is initializing the students and printing the roster. 学生班的主要是做正确的事,它是初始化学生和打印名册。

So you could do two things delete the Roster class or take out this code 所以你可以做两件事删除Roster类或取出这段代码

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());

        }   

}  

to Roster, that will do the job. 到名册,这将完成这项工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM