Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何打印数组列表_Java_Arrays - Fatal编程技术网

Java 如何打印数组列表

Java 如何打印数组列表,java,arrays,Java,Arrays,我正在尝试打印数组列表,但它无法打印。请帮忙 包装公司 导入java.util.ArrayList public class Main { public static void main(String[] args) { ArrayList<Student> students = new ArrayList<>(); String harryPotter = new String(); Student herm

我正在尝试打印数组列表,但它无法打印。请帮忙

包装公司

导入java.util.ArrayList

public class Main {

    public static void main(String[] args) {


        ArrayList<Student> students = new ArrayList<>();
        String harryPotter = new String();
        Student hermoineG = new Student("Hermoine", "Granger", 12);
        System.out.println(hermoineG.getFull());
        Student ronW = new Student("Ron","Weasley",12);
        System.out.println(ronW.getFull());
        Student dracoM = new Student("Draco","Malfoy", 11);
        System.out.println(dracoM.getFull());
        Student lunaL = new Student("Luna", "Lovegood", 9);
        System.out.println(lunaL.getFull());
        Student nevilleL = new Student("Neville", "Longbottom",8);
        System.out.println(nevilleL.getFull());
        Student cedricD = new Student("Cedric","Diggory",12);
        System.out.println(cedricD.getFull());
        Student georgeW = new Student("George", "Weasley",10);
        System.out.println(georgeW.getFull());
        Student padmaP = new Student("Padma", "Patil", 11);
        System.out.println(padmaP.getFull());
        Student fredW = new Student("Fred", "Weasley", 8);
        System.out.println(fredW.getFull());
        School.addStudents(School.students, harryPotter);
        System.out.println(hermoineG.getClass());
        students.add(hermoineG);
        System.out.println(students);

    }
}
公共类主{
公共静态void main(字符串[]args){
ArrayList students=新ArrayList();
String harryPotter=新字符串();
学生hermoineG=新生(“Hermoine”,“Granger”,12);
System.out.println(hermoineG.getFull());
学生ronW=新生(“Ron”,“Weasley”,12);
System.out.println(ronW.getFull());
学生德拉康=新生(“德拉科”,“马尔福”,11);
System.out.println(dracoM.getFull());
学生lunaL=新生(“Luna”,“Lovegood”,9);
System.out.println(lunaL.getFull());
学生内维尔=新学生(“内维尔”,“隆巴顿”,8);
System.out.println(neville.getFull());
学生cedricD=新学生(“Cedric”,“Diggory”,12);
System.out.println(cedricD.getFull());
学生乔治=新生(“乔治”,“韦斯莱”,10);
System.out.println(georgeW.getFull());
学生padmaP=新学生(“Padma”,“Patil”,11);
System.out.println(padmaP.getFull());
学生fredW=新生(“Fred”,“Weasley”,8);
System.out.println(fredW.getFull());
School.addStudents(School.students,harryPotter);
System.out.println(hermoineG.getClass());
学生。添加(hermoineG);
系统输出打印(学生);
}
}

您可能需要使用循环:

for (Student student : students) {
    System.out.println(student.getFull());
}

当您像Student一样创建自己的类时,需要重写Student类中的toString方法,以便使用System.out.println打印。将以下内容添加到学生课堂:

    @Override
    public String toString() {
        return  getFull();
    }
这将允许System.out.println(学生);要正确打印ArrayList。目前,你的名单上只有一个学生


请注意,上面将显示学生对象在“getFull”方法中的格式。如果需要,您可以更改格式。

这是一种特殊的语言吗?你能不能比“它不能打印”更具体一些,比如一条错误消息或其他什么?
String harryPotter=new String()似乎无关紧要。你研究过使用循环吗?它编译吗?学校在哪里?学生在哪里?