【问题标题】:Passing array of objects into another object in java and printing it out java将对象数组传递给java中的另一个对象并将其打印出来java
【发布时间】:2020-04-23 07:00:48
【问题描述】:

我有一个类 Supplement,为此我创建了一系列补充。现在我想将此对象传递给另一个类 Magazine,其中一个杂志的实例有许多补充。所以我希望能够将这个补充对象数组传递到我的 Magazine 构造函数中,并在 Magazine 中编写一个方法,我可以使用它来显示杂志名称、成本以及它存储的补充数组中的所有详细信息。我想我已经成功地将它作为参数传递了,但是我怎样才能编写一个能够在其相关杂志旁边显示该数组的方法?

补充.java:

package javaapplication1;

import java.util.Arrays;

public class Supplement {
    private String supplementname;
    private int WeeklySupCost;
    private Supplement[] supplements;

    public void SetSupplementName(String supplementname){
        this.supplementname = supplementname;  
    };

    public void SetWeeklySupCost(int WeeklySupCost){
        this.WeeklySupCost = WeeklySupCost;
    };

    public String GetSupplementName(){
        return supplementname;
    };
    public int GetWeeklyCost(){
        return WeeklySupCost;
    };

    public Supplement(String supplementname, int WeeklySupCost){
        this.supplementname = supplementname;
        this.WeeklySupCost = WeeklySupCost;     
    };

    public void printSupplements(){
        System.out.println("Supplement: " + this.supplementname);
        System.out.println("Weekly Cost: " + this.WeeklySupCost); 
    }
}

杂志.java:

package javaapplication1;

public class Magazine {
    private String magazinename;
    private int WeeklyCost;
    private Magazine magazineobj;
    private Supplement[] supplements;

    public void SetMagazineName(String magazinename){
        this.magazinename = magazinename;    
    };

    public void SetWeeklyCost(int WeeklyCost){
        this.WeeklyCost = WeeklyCost;
    };

    public String GetMagazineName(){        
        return magazinename;
    };

    public int GetWeeklyCost(){        
        return WeeklyCost;
    };

    public Magazine(String magazinename, int WeeklyCost, Supplement[] supplements){
        this.magazinename = magazinename;
        this.WeeklyCost = WeeklyCost;
        this.supplements = supplements;
    };

    public Magazine(){};
    public void printMagazine(){
        System.out.println("Magazine Name: " + this.magazinename);
        System.out.println("Weekly Cost: " + this.WeeklyCost);
        System.out.println("Supplements: " + supplements[this.supplements]);    
    }
}

主程序:

package javaapplication1;

public class JavaApplication1 {
    public static void main(String[] args) {
        Supplement[] supplements = new Supplement[4];

        supplements[0] = new Supplement("Sports Illustrated Special", 4);
        supplements[1] = new Supplement("Health and Nutrition", 2);
        supplements[2] = new Supplement("Lifestyled", 5);
        supplements[3] = new Supplement("Gamer's Update", 3);

        Magazine magazineobj = new Magazine("The Wheels Special", 35, supplements);
        for(int i = 0; i < 4; i++){
            magazineobj.printMagazine();
            supplements[i].printSupplements();   
        }
        //System.out.println(magazineobj);
    }
}

【问题讨论】:

  • 为什么你的补充类有一个补充数组作为字段?为什么你的杂志班有杂志作为一个领域?同样在java中,方法结束后没有分号
  • 我之前使用它们来尝试创建一个将对象数组作为参数并自动填充一些细节的方法,只是忘记删除它。不要粗鲁,你是对的,但这对我的问题没有任何帮助
  • 当然,这并不是为了解决您的问题,只是一些说明。如果我正确理解您的问题,您想为您的 MagazineSupplement 类实现 toString() 方法。将对象打印到控制台时会调用此方法。

标签: java arrays class object


【解决方案1】:

而不是使用 printMethod() 使用 Object 类的 toString() 方法并在您的类中覆盖。

public class Magazine {

    @Override
    public String toString() {
        String str = "Magazine Name: " + magazinename + "\nWeekly Cost: " + WeeklyCost + "\nSupplements: ";
        for (Supplement supplement : supplements) {
            str += supplement;
        }
        return str;
    }

//    public void printMagazine(){
//        System.out.println("Magazine Name: " + this.magazinename);
//        System.out.println("Weekly Cost: " + this.WeeklyCost);
//        System.out.println("Supplements: " + supplements[this.supplements]);    
//    }

}

class Supplement {

    @Override
    public String toString() {
        return "Supplement: " + supplementname + "\nWeekly Cost: " + WeeklySupCost;
    };

//    public void printSupplements(){
//        System.out.println("Supplement: " + this.supplementname);
//        System.out.println("Weekly Cost: " + this.WeeklySupCost); 
//    }
}

// In Main
//for(int i = 0; i < 4; i++){
//    magazineobj.printMagazine();
//    supplements[i].printSupplements();   
//}
System.out.println(magazineobj);

【讨论】:

  • 这非常适合我!你能解释一下杂志类下toString中for循环背后的逻辑吗?
  • for (Supplement supplement :supplements) { // 遍历一个数组(从supplements中取出每个元素并放入supplements) str +=supplement; // 每个元素补充调用 toString() 方法 }
【解决方案2】:

要打印数组的元素,您可以使用Arrays.toString(arr)。请找到以下代码并检查它是否满足您的要求。

Supplement.java

    import java.util.Arrays;
    public class Supplement {
    private String supplementname;
    private int WeeklySupCost;
    private Supplement[] supplements;

    public void SetSupplementName(String supplementname) {
        this.supplementname = supplementname;
    }

    public void SetWeeklySupCost(int WeeklySupCost) {
        this.WeeklySupCost = WeeklySupCost;
    }

    public String GetSupplementName() {
        return supplementname;
    }

    public int GetWeeklyCost() {
        return WeeklySupCost;
    }

    public Supplement(String supplementname, int WeeklySupCost) {
        this.supplementname = supplementname;
        this.WeeklySupCost = WeeklySupCost;
    }

    public void printSupplements() {
        System.out.println("Supplement: " + this.supplementname);
        System.out.println("Weekly Cost: " + this.WeeklySupCost);
        System.out.println("supplements: " + Arrays.toString(this.supplements));
    }
    @Override
    public String toString() {
        return "Supplement{" +
            "supplementname='" + supplementname + '\'' +
            ", WeeklySupCost=" + WeeklySupCost +
            ", supplements=" + Arrays.toString(supplements) +
            '}';
    }
}

杂志.java

import java.util.Arrays;
public class Magazine {
    private String magazinename;
    private int WeeklyCost;
    private Magazine magazineobj;
    private Supplement[] supplements;

    public void SetMagazineName(String magazinename) {
        this.magazinename = magazinename;
    }

    public void SetWeeklyCost(int WeeklyCost) {
        this.WeeklyCost = WeeklyCost;
    }

    public String GetMagazineName() {
        return magazinename;
    }

    public int GetWeeklyCost() {
        return WeeklyCost;
    }

    public Magazine(String magazinename, int WeeklyCost, Supplement[] supplements) {
        this.magazinename = magazinename;
        this.WeeklyCost = WeeklyCost;
        this.supplements = supplements;
    }

    public Magazine() {
    }

    public void printMagazine() {
        System.out.println("Magazine Name: " + this.magazinename);
        System.out.println("Weekly Cost: " + this.WeeklyCost);
        System.out.println("Supplements: " + Arrays.toString(this.supplements));
    }

}

Main.java

public class Main {
    public static void main(String[] args) {
        Supplement[] supplements = new Supplement[4];
        supplements[0] = new Supplement("Sports Illustrated Special", 4);
        supplements[1] = new Supplement("Health and Nutrition", 2);
        supplements[2] = new Supplement("Lifestyled", 5);
        supplements[3] = new Supplement("Gamer's Update", 3);

        Magazine magazineobj = new Magazine("The Wheels Special", 35, supplements);
        for (int i = 0; i < 4; i++) {
            magazineobj.printMagazine();
            supplements[i].toString();
        }
    }
}

您也可以使用toString()方法打印上述对象的值。

【讨论】:

  • 在我的杂志类中找不到变量“数组”,但是在从下面的答案中对我的两个类调用 toString 方法后,它似乎可以工作
【解决方案3】:

使用ArrayList不是更简单吗?往下看:

Supplement类:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Supplement {
    private String supplementname;
    private int WeeklySupCost;
    public Supplement(String supplementname, int weeklySupCost) {
        this.supplementname = supplementname;
        WeeklySupCost = weeklySupCost;
    }
    public String getSupplementname() {
        return supplementname;
    }
    public void setSupplementname(String supplementname) {
        this.supplementname = supplementname;
    }
    public int getWeeklySupCost() {
        return WeeklySupCost;
    }
    public void setWeeklySupCost(int weeklySupCost) {
        WeeklySupCost = weeklySupCost;
    }
    @Override
    public String toString() {
        return "Supplement [supplementname=" + supplementname + ", WeeklySupCost=" + WeeklySupCost + "]";
    }


}

Magazine类:

public class Magazine {
    private String magazinename;
    private int WeeklyCost;
    private List<Supplement> list;
    public Magazine(String magazinename, int weeklyCost, List<Supplement> list) {
        this.magazinename = magazinename;
        WeeklyCost = weeklyCost;
        this.list = list;
    }
    public String getMagazinename() {
        return magazinename;
    }
    public void setMagazinename(String magazinename) {
        this.magazinename = magazinename;
    }
    public int getWeeklyCost() {
        return WeeklyCost;
    }
    public void setWeeklyCost(int weeklyCost) {
        WeeklyCost = weeklyCost;
    }
    public List<Supplement> getList() {
        return list;
    }
    public void setList(List<Supplement> list) {
        this.list = list;
    }
    @Override
    public String toString() {
        return "Magazine [magazinename=" + magazinename + ", WeeklyCost=" + WeeklyCost + ", list=" + list + "]";
    }



}

JavaApplication1类:

import java.util.ArrayList;
import java.util.List;

public class JavaApplication1 {
    public static void main(String[] args) {
        List<Supplement> supplements = new ArrayList<Supplement>();

        supplements.add(new Supplement("Sports Illustrated Special", 4));
        supplements.add(new Supplement("Health and Nutrition", 2));
        supplements.add(new Supplement("Lifestyled", 5));
        supplements.add(new Supplement("Gamer's Update", 3));

        Magazine magazineobj = new Magazine("The Wheels Special", 35, supplements);

        System.out.println(magazineobj);

        //System.out.println(magazineobj);
    }
}

输出是:

Magazine [magazinename=The Wheels Special, WeeklyCost=35, list=[Supplement [supplementname=Sports Illustrated Special, WeeklySupCost=4], Supplement [supplementname=Health and Nutrition, WeeklySupCost=2], Supplement [supplementname=Lifestyled, WeeklySupCost=5], Supplement [supplementname=Gamer's Update, WeeklySupCost=3]]]

您可以更改toString() 方法以显示您想要的内容,并且您可以大大减少代码。

【讨论】:

  • 这似乎更容易理解,而且效果很好。非常感谢,现在我可以将相同的概念应用于其他具有类似要求的课程
  • 很高兴能帮到你:)
猜你喜欢
  • 1970-01-01
  • 2018-04-16
  • 2021-09-23
  • 1970-01-01
  • 1970-01-01
  • 2015-09-15
  • 2015-02-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多