简体   繁体   English

Java控制台显示对象地址而不是实际值

[英]Java console displaying address of object rather than actual value

Ok so I've got a simple array that I'm working on in Java. 好的,所以我有一个用Java处理的简单数组。 Problem is, when I run the program I get the address of the object rather than the actual value. 问题是,当我运行程序时,我得到的是对象的地址而不是实际值。 I also see to have aa problem with the loop/array. 我也看到循环/数组有一个问题。 It should display houses 3, 5 and 7 but the bottom part is showing 3,4 and 5. Where am I going wrong? 它应该显示房屋3、5和7,但底部显示3,4和5。我要去哪里了? Please see code below, and console output. 请参见下面的代码和控制台输出。 Thanks in advance! 提前致谢!

House[] houses = new House[3];

houses[0] = new House(3,4);
houses[1] = new House(5,7);
houses[2] = new House(7,2);

System.out.println("Number of bottles in house number 3 is: " + houses[0]);
System.out.println("Number of bottles in house number 5 is: " + houses[1]);
System.out.println("Number of bottles in house number 7 is: " + houses[2]);

for (int i = 0; i < houses.length; i++){
  System.out.println("Number of bottles in house " + (i + 3 ) + " is " + houses[i]);
}

Console Output: 控制台输出:

Number of bottles in house number 3 is: org.com1027.lab3.House@d16e5d6 3号房中的瓶子数量是:org.com1027.lab3.House@d16e5d6

Number of bottles in house number 5 is: org.com1027.lab3.House@5a4b4b50 第5号房屋中的瓶子数量是:org.com1027.lab3.House@5a4b4b50

Number of bottles in house number 7 is: org.com1027.lab3.House@53d9f80 7号屋子里的瓶子数目是:org.com1027.lab3.House@53d9f80

Number of bottles in house 3 is org.com1027.lab3.House@d16e5d6 3号房中的瓶子数量是org.com1027.lab3.House@d16e5d6

Number of bottles in house 4 is org.com1027.lab3.House@5a4b4b50 房屋4中的瓶子数量是org.com1027.lab3.House@5a4b4b50

Number of bottles in house 5 is org.com1027.lab3.House@53d9f80 房屋5中的瓶子数是org.com1027.lab3.House@53d9f80

Java do not have any mechanism that magically know how your class should be represented in string format. Java没有任何机制可以神奇地知道您的类应如何以字符串格式表示。 That is why you must implement it by your own. 这就是为什么您必须自己实现它的原因。

You need to override the toString() method to gain "corect" value 您需要重写toString()方法以获取“正确”值

class House {
 //your code

 @Override 
 public String toString() {
   return "The string representation";
 }

}

When you print an instance like house[0] in your example you see the result of the toString method. 当在示例中打印诸如house [0]之类的实例时,您会看到toString方法的结果。 If in your class House toString isn't overriden you see the default implementation which displays the classname and the object-ID of the instance. 如果未在类House toString中覆盖您,则会看到默认的实现,该实现显示实例的类名和对象ID。 So if you want some custom string representation of your House instances you can override the toString() method in class House. 因此,如果您需要House实例的一些自定义字符串表示形式,则可以重写House类中的toString()方法。 There you can eg display the values of the member variables, etc. 在那里,您可以例如显示成员变量的值等。

your for loop iterates from 0 to 2 so your i assumes 0,1,2 and your System.out statement prints i+3 which results in 3,4,5 您的for循环从0迭代到2,因此您的i假设为0,1,2,并且System.out语句显示i + 3,结果为3,4,5

houses[0] is just a reference & it's not the object itself. houses [0]只是一个参考,它不是对象本身。 Just points the object. 只是指向对象。 So, write : 因此,写:

houses[0].getValues();

public String getValues() { return "" + x + "" + y }

or the proper method of house object. 或房屋对象的正确方法。

In your statement, you concatenate a String with a House object. 在您的语句中,您将StringHouse对象连接在一起。 Thus, it will call toString() method of House and substitute its result. 因此,它将调用House toString()方法并替换其结果。

Since you have probably not override this method, it uses default toString() method, which prints class name and object reference. 由于您可能尚未覆盖此方法,因此它使用默认的toString()方法,该方法打印类名和对象引用。

Simply override toString() method in your House class: 只需在House类中重写toString()方法:

public class House {
   // ...

   @Override
   public String toString() {
       return "House: "; // Customize it!
   }

   // ...
}

What you want is to override toString() in your class House . 您要覆盖的是House类中的toString()

class House {
    // ... your implementation, 
    // which I'm guessing it's something like this:
    private int number;
    private int bottles;

    public House(int number, int bottles) {
        this.number = number;
        this.bottles = bottles;
    }

    // here you say what you want as output if you print a House, e.g.:
    @Override 
    public String toString() {
        return String.valueOf(bottles);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java在对象的副本而不是实际的对象上进行同步 - Java synchoronize on copy of object rather then the actual object 为什么println打印引用对象的值而不是对象地址 - Why does println print the value of a referenced object rather than the address of the object Java ArrayList get()为Integer而不是Object - Java ArrayList get() as Integer rather than Object SpringMVC下拉框显示对象信息而不是值 - SpringMVC dropdown box displaying object information rather than values 显示 Java 邮件发件人的地址而不是他的姓名 - Java mail sender's address displayed rather than his name 将Java控制台输出复制到Excel中,行而不是列 - Copying java console output into excel, rows rather than columns 通用对象获取空值而不是双精度值 - Generic Object obtaining null value rather than a double value 如何从适用于多种 Class 类型的方法返回实际类型而不是 Object 类型? - How to return the actual type rather than an Object type from a method that can work for multiple Class types? 获取实际位置而不是模拟位置 - Get actual location rather than mocked location CountResponse.count() 值与 CountResponse 中的实际值不同 object - CountResponse.count() value different than actual value in CountResponse object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM