Java 在tester类中打印数组

Java 在tester类中打印数组,java,Java,我试图返回课程类的tester类中的学生数组,但我一直得到一个.class预期错误。我已经试过了 学生[]。测试课程,但这也不行 public class Course { private String courseName; private String[] students = new String[4]; private int numberOfStudents; public Course(String courseName) { t

我试图返回课程类的tester类中的学生数组,但我一直得到一个.class预期错误。我已经试过了
学生[]。测试课程
,但这也不行

public class Course {
    private String courseName;
    private String[] students = new String[4];
    private int numberOfStudents;
  
    public Course(String courseName) {
        this.courseName = courseName;
    }
  
    public void addStudent(String student) {
        if (numberOfStudents == students.length) {
            String [] copy = new String [students.length*2];
            System.arraycopy(students,0,copy,0,students.length);
            students = copy;
        }
        
        students[numberOfStudents] = student;
        numberOfStudents++;
    }
 
    public String[] getStudents() {
        return students;
    }
 
    public void dropStudent(String student) {
        for (int i=0;i<students.length;i++) {
            if (students[i]==student) {
                students[i] = null;
            }
            for (i=i;i<students.length-1;i++) {
                students[i] = students[i+1];
            }
        }
    }
}

public class TestCourse {
    public static void main() {
        Course compScience = new Course("Computer Science");
        compScience.addStudent("Jack");
        compScience.addStudent("Dean");
        compScience.addStudent("Leon");
        compScience.dropStudent("Dean");
        System.out.println("The students currently in this course are "+ students[]);
    }
}
公共课{
私有字符串courseName;
私有字符串[]学生=新字符串[4];
私人学生;
公共课程(字符串课程名称){
this.courseName=courseName;
}
公共学生(字符串学生){
if(numberOfStudents==students.length){
String[]copy=新字符串[students.length*2];
System.arraycopy(学生,0,副本,0,学生.长度);
学生=复制;
}
学生[人数]=学生;
学生人数++;
}
公共字符串[]getStudents(){
留学生;
}
公共学生(字符串学生){

对于(int i=0;i将
main
方法中的行更改为:

System.out.println(“本课程目前的学生是”+Arrays.toString(compScience.getStudents());
它应该会燃烧起来


您所做的只是在需要通过您创建的引用访问类的字段时尝试直接调用该字段。
Course compScience=new Course(“计算机科学”);
…然后调用它的方法
getStudents()
,如下所示
compScience.getStudents()
。要获取数组的内容,您需要将此方法调用封装在上面的
Arrays.toString()
中。

您可以创建任何您喜欢的方法。您是法官/陪审团/刽子手!当我运行该命令时,我得到“当前在本课程中的学生是[Ljava.lang.String;@56755e13”封装在
Arrays.toString(compScience.getStudents())
以打印元素。