所以我从 arrayList 打印一个对象的名称,它会打印诸如“Tacos@5fefccfd”之类的内容。你能帮我删除这个吗?
我已经尝试过toString()
但问题仍然存在。
System.out.println("Customer#" + orderNum + ": " + table.food.get(choice).toString());
System.out.println("");
for (int j = 0; j < table.food.size(); j++)
{
System.out.print("(" + (j+1) + ")" + table.food.get(j) + " ");
}
System.out.println("");
此代码打印“(1)Burger@1d020199 (2)Fries@30eb6038 (3)Tacos@5fefccfd (4)Nuggets@13826e98”
最佳答案
您正在打印对象的引用。在您的类(class)中放置类似的内容,并将其保存在 table.food 中:
public String toString() {
return this.name;
}
如果类中没有 toString() 方法,则将调用 Object 的 toString() 方法。此方法不知道类的内容,默认情况下它只返回引用。
关于java - 当我在 arrayList 中使用 .get() 方法打印对象时,如何防止打印哈希码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57761999/