java - 坚持从另一个类调用 ArrayList

标签 java arraylist

我看过这里,有类似问题的答案,但我无法使它们符合我的代码。我的代码:

学生类/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());

        }
    }
}

花名册类

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

我遇到的问题是我正在尝试在 Roster 类中执行 System.out.print。我收到的错误是 Roster 类中的“StudentArray 无法解析为变量”。如果我在 StudentArrayList 中的 main 方法下执行此操作,打印方法就可以正常工作。我知道我必须有一种方法来调用数组,只是不确定该怎么做。那会是 setter/getter 吗?

最佳答案

"StudentArray cannot be resolved to a variable"

这意味着您还没有声明变量 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);
        }
    }
}

关于java - 坚持从另一个类调用 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33595329/

相关文章:

java - 在 Eclipse 中使用的最佳 Java 代码生成工具或插件是什么?

java - 编译: Error creating bean with name 'entityManagerFactory'

java - 如何将 Java 对象的实例从 Python 传递到 Java 应用程序

java - 如何实现旋转图像算法和缩放图像算法?

Java SE 1.3,不能使用泛型。我的存储/检索自定义对象的解决方案可以吗?

java - 简单的数学表达式解析

java - 使用 Apache POI Java NetBeans Word 文档时出现 CTPageSZ 类错误

Java 2D 整型数组列表

java - Android Studio, ListView ,java

java - 如何使用特定的类属性对 arraylist asc 进行排序?