【问题标题】:Array output problems?数组输出问题?
【发布时间】:2013-03-31 02:17:14
【问题描述】:

你好 StackOverflow 社区,

我遇到了一些涉及将元素添加到数组中的输出问题。 我在课堂上创建了一个程序,它运行正常,但是当我在自己的计算机上运行相同的程序/代码时,我得到以下输出(有时会生成不同的数字/错误):

“玩具:

toysdemo.ToysDemo@15f5897toysdemo.ToysDemo@b162d5"

为了更清楚,这里是代码:

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
}
}

这是一个真正简单的程序,但令人沮丧的是,正确的输出无法显示。

如果有人能帮助我解决这个难题,我将不胜感激。

谢谢

【问题讨论】:

    标签: java arrays netbeans java-7 output


    【解决方案1】:

    实际上,您正在打印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);
        }
    }
    

    【讨论】:

    • 我明白了。我没有意识到它正在打印参考。感谢您建议 toString 方法。您认为在没有 toString 之前它可以工作的解释是什么?
    • @Naan 运行程序时,JVM 会在堆中的某个位置分配对象,并为每个对象分配一个不同的地址。每次运行程序时,您应该会看到对象的不同 地址,从而解释旧的行为。也许您的班级已经定义了 toString 方法,而您可能将其清除了,或者您使用的是 int[]String[] 数组而不是 ToysDemo[] 数组。
    【解决方案2】:

    当您调用System.out.print(someToy) 时,它会调用someToy.toString() 并打印结果。
    如果你不覆盖toString(),你会得到默认的Object.toString(),它会打印类名和内存地址。

    【讨论】:

    • 感谢您的评论。我之前没有 toString 并且它正确显示了输出。你知道为什么会这样吗?
    • @Naan:因为您正在打印其他内容(例如,getName()
    【解决方案3】:

    您需要将函数 toString 添加到 ToysDemo 类中。例如:

    @Override
    public String toString()
    {
       return "Name: "+name+"\tPrice: "+price;
    }
    

    【讨论】:

      【解决方案4】:

      您需要重写 Object 类的 toString() 方法。如果您不这样做,JVM 将执行基类方法,该方法默认打印类的完全限定名称,即带有包名称的类名称和存储对象的内存地址,这就是您获得该输出的方式。现在,当您调用 system.out.print 时,它将转到覆盖的方法并实现它。

      例如:

      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); 
          } 
      } 
      

      【讨论】:

      • 这是一个示例 class Employee { private String name;私人年龄; 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(String args[]) { Employee e = new Employee(); e.setName("罗伯特"); e.setAge(20); System.out.println(e); } } o/p: 员工的名字是罗伯特,年龄是 20 谢谢
      猜你喜欢
      • 2021-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-25
      • 2022-11-26
      • 2013-12-29
      • 2022-01-05
      • 1970-01-01
      相关资源
      最近更新 更多