Java 阵列输出问题?
大家好,我的社区 我在一些涉及向数组中添加元素的输出中遇到问题。 我在课堂上创建了一个程序,它运行正常,但当我在自己的计算机上运行相同的程序/代码时,我得到以下输出(有时会生成不同的数字/错误): “玩具: 玩具演示。ToysDemo@15f5897toysdemo.ToysDemo@b162d5“ 为了更清楚地说明这一点,以下是代码:Java 阵列输出问题?,java,arrays,netbeans,java-7,output,Java,Arrays,Netbeans,Java 7,Output,大家好,我的社区 我在一些涉及向数组中添加元素的输出中遇到问题。 我在课堂上创建了一个程序,它运行正常,但当我在自己的计算机上运行相同的程序/代码时,我得到以下输出(有时会生成不同的数字/错误): “玩具: 玩具演示。ToysDemo@15f5897toysdemo.ToysDemo@b162d5“ 为了更清楚地说明这一点,以下是代码: package toysdemo; public class ToysDemo { private float price; private String
package toysdemo;
public class ToysDemo {
private float price;
private String name;
public float getPrice(){
return price;
}
public void setPrice(float newPrice){
price = newPrice;
}
public String getName() {
return name;
}
public void setName(String newName) {
name = newName;
}
public static void printToys(ToysDemo arrayOfToys[], int size) {
//display content of array
System.out.println("The toys: ");
for (int i = 0; i < size; i++) {
System.out.print(arrayOfToys[i]);
}
System.out.println();
}//print toys
public static void main(String[] args) {
ToysDemo arrayOfToys[] = new ToysDemo[5];
int numberOfToys = 0;
// create two toys and save into array
ToysDemo toy = new ToysDemo();
toy.setPrice((float)111.99);
toy.setName("Giant Squid");
arrayOfToys[numberOfToys++] = toy;
ToysDemo toy2 = new ToysDemo();
toy2.setPrice((float)21.99);
toy2.setName("small Squid");
arrayOfToys[numberOfToys++] = toy2;
//print toys into array
printToys(arrayOfToys, numberOfToys); //the call
}
}
包装玩具演示;
公营玩具演示{
私人浮动价格;
私有字符串名称;
公开发行价格(){
退货价格;
}
公共无效设定价格(浮动新价格){
价格=新价格;
}
公共字符串getName(){
返回名称;
}
public void setName(字符串newName){
name=newName;
}
公共静态void打印玩具(ToysDemo arrayOfToys[],内部尺寸){
//显示数组的内容
System.out.println(“玩具:”);
对于(int i=0;i
这是一个非常简单的程序,但是如何显示正确的输出却令人沮丧
如果有人能帮我解决这个难题,我将不胜感激
谢谢实际上,您正在打印
ToysDemo
对象的引用。为了使System.out.println(arrayOfToys[i])
工作,您的ToysDemo
类需要重写toString
方法
示例代码:
public class ToysDemo {
//class content...
@Override
public String toString() {
return "My name is: " + name + " and my price is: " + String.format("%.2f", price);
}
}
实际上,您正在打印
ToysDemo
对象的引用。为了使System.out.println(arrayOfToys[i])
工作,您的ToysDemo
类需要重写toString
方法
示例代码:
public class ToysDemo {
//class content...
@Override
public String toString() {
return "My name is: " + name + " and my price is: " + String.format("%.2f", price);
}
}
当您调用
System.out.print(someToy)
时,它调用someToy.toString()
并打印结果。如果不重写
toString()
,则会得到默认的对象.toString()
,该对象会打印类名和内存地址。当调用System.out.print(someToy)
时,它会调用someToy.toString()
并打印结果。如果不重写
toString()
,则会得到默认的对象.toString()
,它会打印类名和内存地址。您需要将函数toString添加到ToysDemo类中。例如:
@Override
public String toString()
{
return "Name: "+name+"\tPrice: "+price;
}
Employee {
private String name;
private int age;
public void setName(String name) { this.name = name; }
public String getName() { return this.name; }
public void setAge(int age) { this.age = age; }
public int getAge() { return this.age = age; }
@Override
public String toString() {
return "Name of the employee is " + name + " and age is " + age;
}
public static void main(String args[]) {
Employee e = new Employee();
e.setName("Robert");
e.setAge(20);
System.out.println(e);
}
}
您需要将函数toString添加到ToysDemo类中。例如:
@Override
public String toString()
{
return "Name: "+name+"\tPrice: "+price;
}
Employee {
private String name;
private int age;
public void setName(String name) { this.name = name; }
public String getName() { return this.name; }
public void setAge(int age) { this.age = age; }
public int getAge() { return this.age = age; }
@Override
public String toString() {
return "Name of the employee is " + name + " and age is " + age;
}
public static void main(String args[]) {
Employee e = new Employee();
e.setName("Robert");
e.setAge(20);
System.out.println(e);
}
}
您需要重写对象类的toString()方法。如果不这样做,JVM将执行基类方法,默认情况下,基类方法将打印类的完全限定字段名,即类名、包名和存储对象的内存地址,这就是获得输出的方式。现在,当调用system.out.print时,它将转到覆盖的方法并实现它 例如:
@Override
public String toString()
{
return "Name: "+name+"\tPrice: "+price;
}
Employee {
private String name;
private int age;
public void setName(String name) { this.name = name; }
public String getName() { return this.name; }
public void setAge(int age) { this.age = age; }
public int getAge() { return this.age = age; }
@Override
public String toString() {
return "Name of the employee is " + name + " and age is " + age;
}
public static void main(String args[]) {
Employee e = new Employee();
e.setName("Robert");
e.setAge(20);
System.out.println(e);
}
}
您需要重写对象类的toString()方法。如果不这样做,JVM将执行基类方法,默认情况下,基类方法将打印类的完全限定字段名,即类名、包名和存储对象的内存地址,这就是获得输出的方式。现在,当调用system.out.print时,它将转到覆盖的方法并实现它 例如:
@Override
public String toString()
{
return "Name: "+name+"\tPrice: "+price;
}
Employee {
private String name;
private int age;
public void setName(String name) { this.name = name; }
public String getName() { return this.name; }
public void setAge(int age) { this.age = age; }
public int getAge() { return this.age = age; }
@Override
public String toString() {
return "Name of the employee is " + name + " and age is " + age;
}
public static void main(String args[]) {
Employee e = new Employee();
e.setName("Robert");
e.setAge(20);
System.out.println(e);
}
}
我懂了。我不知道这是在打印参考资料。感谢您推荐toString方法。您认为它在没有toString之前工作的原因是什么?@Naan当您运行程序时,JVM将在堆中的某个位置分配对象,并为每个对象分配不同的地址。每次运行程序时,您都会看到对象的不同地址,从而解释旧的行为。可能您的类已经定义了
toString
方法,您可能已经删除了它,或者您使用的是int[]
或String[]
数组而不是ToysDemo[]
数组。我明白了。我不知道这是在打印参考资料。感谢您推荐toString方法。您认为它在没有toString之前工作的原因是什么?@Naan当您运行程序时,JVM将在堆中的某个位置分配对象,并为每个对象分配不同的地址。每次运行程序时,您都会看到对象的不同地址,从而解释旧的行为。可能您的类已经定义了toString
方法,您可能将其删除,或者您使用的是int[]
或String[]
数组而不是ToysDemo[]
数组。谢谢您的评论。我以前没有使用字符串,它正确地显示了输出。你知道为什么会这样吗?@Naan:因为你正在打印其他东西(例如,getName()
),谢谢你的评论。我以前没有使用字符串,它正确地显示了输出。你知道为什么会这样吗?@Naan:因为你在打印其他东西(例如,getName()
),这里有一个示例类Employee{private String name;private int age;public void setName(String name){this.name=name;}public String getName(){return this.name;}public void setAge(int age){this.age=age;}public int getAge(){return this.age=age;}public String toString(){return“员工的姓名为“+Name+”,年龄为“+age;}public static void main(字符串args[]){employee e=new employee