Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.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 为什么我无法接收arraylist的内容_Java_Arraylist - Fatal编程技术网

Java 为什么我无法接收arraylist的内容

Java 为什么我无法接收arraylist的内容,java,arraylist,Java,Arraylist,我目前正在努力修复代码的结果 我应该从菜单中添加一个列表,然后显示该列表。然而,我无法检索它的内容,而是接收它的内存值 学生班 private int number; private String author; private String title; public Student() { } public Student(int number, String title, String author) { this.num

我目前正在努力修复代码的结果

我应该从菜单中添加一个列表,然后显示该列表。然而,我无法检索它的内容,而是接收它的内存值

学生班

    private int number;
    private String author;
    private String title;

    public Student() {
    }

    public Student(int number, String title, String author) {
        this.number = number;
        this.title = title;
        this.author = author;
    }

    public int getNumber() {
        return number;
    }

    public String getTitle() {
        return title;
    }

    public String getAuthor() {
        return author;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String ToString() {
        return "Number: " + number + "\tTitle: " + title + "\tAuthor: " + author;
    }
主类

import java.util.*;

public class Main {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        ArrayList<Student> newStudents = new ArrayList<Student>();

        System.out.println("Please select a number from the options below \n");

        while (true) {
            // Give the user a list of their options
            System.out.println("1: Add a student to the list.");
            System.out.println("2: Remove a student from the list.");
            System.out.println("3: Display all students in the list.");


            // Get the user input

            int userChoice = input.nextInt();
            switch (userChoice) {
                case 1:
                    addStudents(newStudents);
                    break;
                case 2:
                    //removeStudent(newStudents);
                    break;
                case 3:
                    displayStudent(newStudents);
                    break;
            }
        }
    }

    public static void addStudents(ArrayList<Student> newStudents) {


        Scanner input = new Scanner(System.in);

        Student newStudent = new Student();

        System.out.print("Please enter number: ");
        newStudent.setNumber(input.nextInt());

        System.out.print("Please enter title: ");
        newStudent.setTitle(input.next());

        System.out.print("Please enter author: ");
        newStudent.setAuthor(input.next());

        if (newStudents.size() <= 100) {
            newStudents.add(newStudent);

            System.out.println("Student added\n");
        } else {
            System.out.println("\n Student interface is full!");
        }

    }

}

    private static void displayStudent(ArrayList<Student> newStudents) {


        for (Student e : newStudents) {
            System.out.println(e);
        }
    }
}
输出:

1:将一名学生添加到列表中

2:从列表中删除一名学生

3:显示列表中的所有学生

三,

Student@6b2acb7a

为什么是@6b2babc7a

谢谢你的帮助和关注。我对编程基本上是新手,Java是我的第一语言。因此,我非常感谢您的帮助和澄清。

您必须在学生课堂上重写public String to String,以便在System.out.println中使用时提供String

但您有publicstringtostring将其更改为publicstringtostring


如果学生中没有toString方法,则将调用java.lang.Object中的toString方法,该方法将返回实例的哈希代码。

在java中调用print any Object时,将在内部调用该类的toString方法。正如在Java中一样,对象类是所有类的父类,而toString方法在对象类中可用。因此,此方法适用于所有类对象

默认情况下,对象上的toString返回getClass.getName+'@'+Integer.toHexStringhashCode

因此,你得到了Student@6b2acb7a作为输出。如果要打印其他内容,则需要重写学生类中的toString,并且从该方法返回的任何内容都将得到print

对象类中的方法名为toString。因此,您需要像这样做:

@Override
public String toString() {
        return "Number: " + number + "\tTitle: " + title + "\tAuthor: " + author;
    }
要点:当您重写超类中的任何方法时,请使用@override注释对其进行注释。如果不正确地重写它,则会出现编译错误。在编译时发现问题总是比在运行时更好。如果你这样做,你就会发现问题所在