第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

為什么我無(wú)法接收arraylist的內(nèi)容

為什么我無(wú)法接收arraylist的內(nèi)容

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

2 回答

?
富國(guó)滬深

TA貢獻(xiàn)1790條經(jīng)驗(yàn) 獲得超9個(gè)贊

當(dāng)您在 Java 中調(diào)用打印任何對(duì)象時(shí),toString()會(huì)調(diào)用該類的內(nèi)部方法。正如在 Java 中一樣,Object 類是所有類的父類,并且toString()方法在 Object 類中可用。所以這個(gè)方法對(duì)所有Class對(duì)象都是可用的。


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


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


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


@Override

public String toString() {

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

    }

重要提示:當(dāng)您重寫(xiě)超類中的任何方法時(shí),請(qǐng)使用@Override注釋對(duì)其進(jìn)行注釋。如果您錯(cuò)誤地覆蓋它,您將收到編譯錯(cuò)誤。在編譯時(shí)發(fā)現(xiàn)問(wèn)題總是比在運(yùn)行時(shí)發(fā)現(xiàn)問(wèn)題更好。如果你這樣做了,你就會(huì)發(fā)現(xiàn)問(wèn)題了。


查看完整回答
反對(duì) 回復(fù) 2023-07-13
?
慕容森

TA貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超18個(gè)贊

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

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

如果沒(méi)有 outtoString()方法,則將調(diào)用from 方法Student,該方法將返回實(shí)例的哈希碼。toString()java.lang.Object


查看完整回答
反對(duì) 回復(fù) 2023-07-13
  • 2 回答
  • 0 關(guān)注
  • 129 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)