亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么我無法接收arraylist的內容

為什么我無法接收arraylist的內容

慕勒3428872 2023-07-13 15:32:57
我目前正在努力修復代碼的結果。我應該從菜單添加一個列表,然后顯示該列表。但是,我無法檢索其內容,而是收到其內存值(我猜?)。學生班    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;            }        }    }輸出:1:將學生添加到列表中。2:從列表中刪除學生。3:顯示列表中的所有學生。3學生@6b2acb7a為什么@6b2babc7a?感謝您的善意幫助和關注。我對編程還算陌生,Java 是我的第一語言。因此,我非常感謝您的幫助和澄清。
查看完整描述

2 回答

?
富國滬深

TA貢獻1790條經驗 獲得超9個贊

當您在 Java 中調用打印任何對象時,toString()會調用該類的內部方法。正如在 Java 中一樣,Object 類是所有類的父類,并且toString()方法在 Object 類中可用。所以這個方法對所有Class對象都是可用的。


默認情況下 toString() 對象返回getClass().getName() + '@' + Integer.toHexString(hashCode())。


因此,您將得到Student@6b2acb7a作為輸出。如果您想打印其他內容,則需要重寫toString()Student 類中的 ,并且return從該方法中獲得的任何內容都將得到打印。


Object 類中的方法名為 toString()。所以你需要這樣做:


@Override

public String toString() {

        return "Number: " + number + "\tTitle: " + title + "\tAuthor: " + author;

    }

重要提示:當您重寫超類中的任何方法時,請使用@Override注釋對其進行注釋。如果您錯誤地覆蓋它,您將收到編譯錯誤。在編譯時發現問題總是比在運行時發現問題更好。如果你這樣做了,你就會發現問題了。


查看完整回答
反對 回復 2023-07-13
?
慕容森

TA貢獻1853條經驗 獲得超18個贊

您必須public String toString()在 Student 類中重寫以在使用時提供 StringSystem.out.println()

但你已經public String ToString()將其更改為public String toString().

如果沒有 outtoString()方法,則將調用from 方法Student,該方法將返回實例的哈希碼。toString()java.lang.Object


查看完整回答
反對 回復 2023-07-13
  • 2 回答
  • 0 關注
  • 130 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號