哈希code到位数组元素的印 [英] Hash code printed in place of array elements

查看:58
本文介绍了哈希code到位数组元素的印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个新的类名为学生,设置和获取不同的变量,如姓名,GPA,如果他们入选,他们注册的时候(这是一个字符串,它的目的是)的程序。问题是,当我尝试将信息存储到一个数组和/或数组列表并打印我得到的哈希值code打印的,而不是信息。该计划还没有完全完成,但我想首先解决哈希code问题。如果你能帮助我更AP preciate。谢谢你在前进!

 学生ST =新学生();
    的System.out.println(st.getName());
    Student1();
    学生ST1 =新的学生(约翰);}公共静态无效Student1(){    的System.out.println(学生1输入数据:);
    学生ST =新学生();
    扫描仪键盘=新的扫描仪(System.in);    字符串名称= keyboard.next();
    双GPA = keyboard.nextDouble();
    布尔招收= keyboard.nextBoolean();
    字符串日期= keyboard.next();
     的for(int i = 0;我小于5;我++){
    学生newStudent =新学生();
    newStudent.setName(名);
    newStudent.setGPA(GPA);
    newStudent.setEnrolled(登记);
    newStudent.setDate(日期);
    ArrayList的<的Student GT; strList =新的ArrayList<的Student GT;();
    学生学生[] =新的学生[5];
    newStudent.getName();
    学生[i] = newStudent;    strList.add(newStudent);
    的System.out.println(学生[I]);

}

学生班级

公共类学生{

 私人字符串名称;
私人双GPA;
私人布尔录取;
私人字符串enrollDate;公益助学(){    this.name =;
    this.gpa = 0.0;
    this.enrolled = FALSE;
    this.enrollDate =无;}公益助学(字符串名称1){
    this.name =名称1;
    this.gpa = 0.0;
    this.enrolled = FALSE;
    this.enrollDate =无;
}公共无效setname可以(字符串名称){
    this.name =名称;
}公共字符串的getName(){
    返回this.name;
}公共无效setGPA(双GPA){
    this.gpa = GPA;
}公共双getGPA(){
    返回this.gpa;
}公共无效setEnrolled(布尔就读){
    this.enrolled =登记的;
}公共布尔getEnrolled(){
    返回this.enrolled;
}公共无效的setDate(字符串日期){
}

}


解决方案

  1. 如果你想有一个重新的字符串对象的presentation,这个对象必须实现的toString()。如果你没有在JVM中的默认设置是打印哈希code。

  2. 如果您想打印对象的特定领域,你应该明确地打印领域,通过在必要的方法;像的System.out.println(学生[I] .getName());

  3. 如果你想打印整个数组,你应该使用 Arrays.toString()

I created a program with a new class called Student that sets and gets different variables such as name, GPA, if they are enrolled and when they are enrolled (this is a String, it is intended to be). The problem is that when I try to store the information into an array and/or array list and print it I get the hash code printed instead of the information. The program is not entirely complete, but I am trying to address the hash code issue first. If you could assist I would much appreciate. Thank you in advance!

    Student st = new Student();


    System.out.println(st.getName());
    Student1();
    Student st1 = new Student("John");

}

public static void Student1() {

    System.out.println("Enter the data for student 1 :");
    Student st = new Student();
    Scanner keyboard = new Scanner(System.in);

    String name = keyboard.next();
    double GPA = keyboard.nextDouble();
    boolean enrolled = keyboard.nextBoolean();
    String date = keyboard.next();
     for (int i =0; i<5; i++){
    Student newStudent = new Student();
    newStudent.setName(name);
    newStudent.setGPA(GPA);
    newStudent.setEnrolled(enrolled);
    newStudent.setDate(date);
    ArrayList <Student> strList = new ArrayList<Student>();
    Student student[] = new Student[5];
    newStudent.getName();
    student[i] = newStudent;

    strList.add(newStudent);
    System.out.println(student[i]);

}

Student Class

public class Student {

private String name;
private double gpa;
private boolean enrolled;
private String enrollDate;

public Student() {

    this.name = "";
    this.gpa = 0.0;
    this.enrolled = false;
    this.enrollDate = "none";

}

public Student(String name1) {
    this.name = name1;
    this.gpa = 0.0;
    this.enrolled = false;
    this.enrollDate = "none";
}

public void setName(String name) {
    this.name = name;
}

public String getName() {
    return this.name;
}

public void setGPA(double gpa) {
    this.gpa = gpa;
}

public double getGPA() {
    return this.gpa;
}

public void setEnrolled(boolean enrolled) {
    this.enrolled = enrolled;


}

public boolean getEnrolled() {
    return this.enrolled;
}

public void setDate(String date) {
}

}

解决方案

  1. If you want a String representation of an object, this object must implement toString(). If you don't the default in the JVM is to print the hash code.
  2. If you want to print a specific field of your object, you should explicitly print that field, through a method if necessary; like System.out.println(student[i].getName());
  3. If you are trying to print a whole array, you should use Arrays.toString().

这篇关于哈希code到位数组元素的印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆