【问题标题】:Store objects in an array and print it out JAVA将对象存储在数组中并打印出来 JAVA
【发布时间】:2017-07-07 14:48:46
【问题描述】:

我已经创建了不同类型的对象,例如 object1 object2 等。现在,如何将这些对象存储到一个数组中并打印出该数组?下面的想法不起作用。

Buch buch = new Buch("Blablabla", "Baba Isi", "DE 1234567890", 50, 1234f); 
    System.out.println(buch.toString()); 
    //5 Objekte erstellen
    /*2*/Buch buch2 = new Buch("Blabla2", "blubb", "DE 5461563259", 50, 1234f); 
    System.out.println(buch2.toString()); 
    /*3*/Buch buch3 = new Buch("Check this out", "oke", "DE 67892011789", 50, 1234f); 
    System.out.println(buch3.toString()); 
    /*4*/Buch buch4 = new Buch("Got ya", "catch ya", "DE 678198771890", 50, 1234f); 
    System.out.println(buch4.toString()); 
    /*5*/Buch buch5 = new Buch("IDK", "IDK2", "DE 47740710974691", 50, 1234f); 
    System.out.println(buch5.toString()); 

    Buch[] arr = new Buch[5]; 
    for(int i = 0; i<5; i++){
        arr[i] = new Buch(); 
    }
    for(int i = 0; i < arr.length; i++){      //toString method exists an it works
        System.out.println(arr[i].toString()); 
    }

输出:

标题:Ein Mann ein Wort 作者:Baba Isi ISBN:DE 1234567890 Seitenzahl:50 Preis:1234.0

标题:想粉碎作者:粉碎它 ISBN:DE 5461563259 Seitenzahl:50 Preis:1234.0

标题:Smash Me 作者:oke ISBN:DE 67892011789 Seitenzahl:50 Preis:1234.0

标题:Got ya Autor:catch ya ISBN:DE 678198771890 Seitenzahl:50 Preis:1234.0

标题:IDK 作者:IDK2 ISBN:DE 47740710974691 Seitenzahl:50 Preis:1234.0

标题:IDK 作者:IDK2 ISBN:DE 47740710974691 Seitenzahl:50 Preis:1234.0

标题:IDK 作者:IDK2 ISBN:DE 47740710974691 Seitenzahl:50 Preis:1234.0

标题:IDK 作者:IDK2 ISBN:DE 47740710974691 Seitenzahl:50 Preis:1234.0

标题:IDK 作者:IDK2 ISBN:DE 47740710974691 Seitenzahl:50 Preis:1234.0

标题:IDK 作者:IDK2 ISBN:DE 47740710974691 Seitenzahl:50 Preis:1234.0

【问题讨论】:

  • 您的代码应该可以工作。你得到什么错误?
  • 我有一个错误,因为我忘记了“;”在for循环中打印出数组。现在我修复了它,我的代码有输出:
  • 我有一种预感,你的字段都被声明为static
  • 寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:How to create a Minimal, Complete, and Verifiable example

标签: java arrays object tostring


【解决方案1】:

您已经在构造函数中为对象提供了数据。实际上,当您创建新数组时,您正在生成不包含字符串的新对象。

要解决这个问题,您可以在另一个函数中提供对象的参数,并在生成后为每个数组项调用该函数,如下所示:

        Buch[] arr = new Buch[5]; 
        for(int i = 0; i<5; i++){
            arr[i] = new Buch(); 
        }

        arr[0].fill("Blablabla", "Baba Isi", "DE 1234567890", 50, 1234f); 
        arr[1].fill("Blabla2", "blubb", "DE 5461563259", 50, 1234f); 
        arr[2].fill("Check this out", "oke", "DE 67892011789", 50, 1234f); 
        arr[3].fill("Got ya", "catch ya", "DE 678198771890", 50, 1234f); 
        arr[4].fill("IDK", "IDK2", "DE 47740710974691", 50, 1234f);


        for(int i = 0; i < arr.length; i++){      //toString method exists an it works
            System.out.println(arr[i].toString()); 
        }

【讨论】:

    【解决方案2】:

    用类似的东西替换 for 循环:

        Buch[] arr = { buch, buch2, buch3, buch4, buch5 };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多