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

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

如何覆蓋它以獲得結構化的方式

如何覆蓋它以獲得結構化的方式

慕森王 2022-05-12 17:25:07
我正在使用 Maps 集合創建學生管理簡單的 java 項目,其中 id 是我的密鑰,名稱、標記和手機號碼。是地圖的值。那么如何以結構化的方式打印它。HashMap<Integer, LinkedHashSet<StudentCinstructor>> st = new HashMap<>();LinkedHashSet<StudentCinstructor> st2 = new LinkedHashSet<>();Scanner sc = new Scanner(System.in);public void add() {    System.out.println("enter the name of the student");    String name = sc.nextLine();    System.out.println("enter the marks of the student");    double marks = sc.nextDouble();    System.out.println("enter the mobile number of the student");    long mobile_no = sc.nextLong();    st2.add(new StudentCinstructor(name, marks, mobile_no));    System.out.println("enter the unique id of the student");    int id = sc.nextInt();    st.put(id, st2);當我嘗試在主方法中打印自定義類時,它給了我一個帶有哈希碼的地址?!癏ashmapDemo.MethodsForManagement@3d4eac69”
查看完整描述

2 回答

?
藍山帝景

TA貢獻1843條經驗 獲得超7個贊

兩個備注:

1-當您嘗試打印對象StudentCinstructor時,如果沒有專門的toString()方法,您將無法獲得結構良好的輸出。因此,您需要做的是toString()為您的類編寫一個方法,然后您可以打印到控制臺。例子 :

public static String toString() {
     return "Customize here + Put this method inside your class";
     }

2-我不明白您為什么要使用LinkedHashSet存儲StudentCinstructor對象然后將此 HashSet 存儲在地圖中,而不是創建StudentCinstructor對象并將其直接存儲在地圖中,如果所有學生都有唯一的 id。如 :

HashMap<Integer, StudentCinstructor> st = new HashMap<>();


查看完整回答
反對 回復 2022-05-12
?
猛跑小豬

TA貢獻1858條經驗 獲得超8個贊

查看您的打印輸出“HashmapDemo.MethodsForManagement@3d4eac69”,您似乎正在打印 class 的對象HashmapDemo.MethodsForManagement。如果要打印 的對象StudentCinstructor,則需要將該對象傳遞給 print 方法,例如System.out.println(student);.


你需要重寫類中的toString()方法StudentCinstructor。(即把下面的代碼放在StudentCinstructor課堂上。)


( name,marks并且mobile_no在下面的代碼中是StudentCinstructor類中的字段。)


  @Override

  public String toString()

  {

    return "Name=" + name + ", Marks=" + marks + ", Mobile number=" + mobile_no;

  }


查看完整回答
反對 回復 2022-05-12
  • 2 回答
  • 0 關注
  • 153 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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