有没有一种方法可以使用(toString)打印哈希码值

时间:2020-04-17 18:54:23

标签: java tostring treemap treeset

import java.io.*;

import java.util.*;
class Student implements Comparable<Student>{
String name;
int rno;
Student(String name, int rno){
    this.name = name;
    this.rno = rno;
} // end of constructor
public boolean equals(Object o){
    Student st = (Student)o;
    return rno == st.rno;
}
public int compareTo(Student s){
    return rno-s.rno;
}
public String toString(){
    return name+" "+rno;
}
}   

class demo{
public static void main(String args[]){
    Console c = System.console();
    int marks [] = new int[4];
    TreeSet <Student> sinfo = new TreeSet<Student>();
    TreeMap<Student,int[]> full_info = new TreeMap<>();
    int no_student = Integer.parseInt(c.readLine("Enter the number of students: "));
        for(int j = 0;j<no_student;j++){
            String sname = c.readLine("Enter"+(j+1)+" Student name: ");
            int srno = Integer.parseInt(c.readLine("Enter "+sname+"'s Roll.No. : "));
            Student s = new Student(sname,srno);
            sinfo.add(s);
            int english = Integer.parseInt(c.readLine("Enter the marks of English: "));
            int maths = Integer.parseInt(c.readLine("Enter the marks of Maths: "));
            int hindi = Integer.parseInt(c.readLine("Enter the marks of Hindi: "));
            int sci = Integer.parseInt(c.readLine("Enter the marks of Science: "));
                for(int i = 0; i<marks.length; i++){
                if(i == 0){marks[i]= english;}
                    else if(i == 1){marks[i]= maths;}
                        else if(i == 2){marks[i]=hindi;}
                            else{marks[i] = sci;}
                }// end of for for marks entry
        full_info.put(s,marks); 
        }
for (Map.Entry<Student, int[]> 
             entry : full_info.entrySet()) 
        System.out.println( 
            "[" + entry.getKey() 
            + ", " + entry.getValue() + "]");

}// end of main
}// end of demo

这是输出结果

  1. D:\ java实践> java演示
  2. 输入学生人数:1
  3. 输入1个学生姓名:swapnil
  4. 输入swapnil的Roll。 :101
  5. 输入英文标记:10
  6. 输入数学的分数:20
  7. 输入印地语标记:30
  8. 输入科学分数:40
  9. [swapnil 101,[I @ 340f438e]

我应该进行哪些更改才能使输出具有[10,20,30,40]而不是哈希码? 我应该在哪里覆盖toString方法以获取值?

0 个答案:

没有答案