ArrayList用于在Java中存储对象,但当我添加对象时却无法正常工作。

huangapple go评论89阅读模式
英文:

ArrayList for storing objects in Java isn't working when I add objects

问题

我目前正在解决来自 https://java-programming.mooc.fi/part-6/1-objects-within-objects 的练习,题目名为 "货舱"。我应该将一个类的对象存储到另一个类的 ArrayList 中,但无论何时我使用 this.suitcases.add(suitcase) 方法,它都不会将对象添加进去,当我打印时也看不到。我该如何修复这个问题?任何帮助都将不胜感激。谢谢!

程序的输出应该如下:
2 个行李箱(7 公斤)

我的输出:
0 个行李箱(0 公斤)

public class Hold {
    
    private int minWeight;
    private int maxWeight;
    private ArrayList<Suitcase> suitcases;
    
    public Hold(int maxWeight){
        this.minWeight = 0;
        this.maxWeight = maxWeight;
        this.suitcases = new ArrayList<>();
    }
    
    public void addSuitcase(Suitcase suitcase){
        if(suitcase.totalWeight() + minWeight >= maxWeight){
            minWeight = minWeight + suitcase.totalWeight();
            this.suitcases.add(suitcase);
        }
    }
    
    public String toString(){
        return this.suitcases.size() + " 个行李箱(" + minWeight + " 公斤)";
    }
}
public class Suitcase {

    private int maxWeight;
    private int minWeight;
    private ArrayList<Item> items;

    public Suitcase(int maxWeight) {
        this.minWeight = 0;
        this.maxWeight = maxWeight;
        this.items = new ArrayList<>();
    }

    public void addItem(Item item) {

        if (minWeight + item.getWeight() <= maxWeight) {
            minWeight = minWeight + item.getWeight();
            this.items.add(item);
        }
    }

    public void printItems() {
        for (Item item : this.items) {
            System.out.println(item);
        }
    }

    public Item heaviestItem() {
        if (this.items.isEmpty()) {
            return null;
        }

        Item returnObject = this.items.get(0);

        for (Item i : this.items) {
            if (returnObject.getWeight() < i.getWeight()) {
                returnObject = i;
            }
        }

        return returnObject;

    }

    public int totalWeight() {
        return minWeight;
    }

    public String toString() {
        if (this.items.isEmpty()) {
            return "没有物品(0 公斤)";
        }
        if (this.items.size() == 1) {
            return this.items.size() + " 个物品(" + minWeight + " 公斤)";
        }

        return this.items.size() + " 个物品(" + minWeight + " 公斤)";
    }
}
public class Item {

    private String name;
    private int weight;
    
    public Item(String name, int weight){
        this.name = name;
        this.weight = weight;
    }
    
    public String getName(){
        return this.name;
    }
    
    public int getWeight(){
        return this.weight;
    }
    
    public String toString(){
        return this.name + "(" + this.weight + " 公斤)";
    }
}
public class Main {

    public static void main(String[] args) {
        // 你可以使用主方法来测试你的类!
        Item book = new Item("指环王", 2);
        Item phone = new Item("诺基亚 3210", 1);
        Item brick = new Item("砖头", 4);

        Suitcase adasCase = new Suitcase(10);
        adasCase.addItem(book);
        adasCase.addItem(phone);

        Suitcase pekkasCase = new Suitcase(10);
        pekkasCase.addItem(brick);

        Hold hold = new Hold(1000);
        hold.addSuitcase(adasCase);
        hold.addSuitcase(pekkasCase);

        System.out.println(hold);
    }

}
英文:

I'm currently solving an exercise from https://java-programming.mooc.fi/part-6/1-objects-within-objects Exercise: Cargo Hold. I'm supposed to store objects of a class to an ArrayList of another class but whenever I use the this.suitcases.add(suitcase) method it never adds the object when I print it. How can I fix this? Any help would surely be appreciated. Thanks!

>The program's output should be the following:
>>2 suitcases (7 kg)
>
>My output:
>>0 suitcases (0 kg)

public class Hold {
private int minWeight;
private int maxWeight;
private ArrayList&lt;Suitcase&gt; suitcases;
public Hold(int maxWeight){
this.minWeight = 0;
this.maxWeight = maxWeight;
this.suitcases = new ArrayList&lt;&gt;();
}
public void addSuitcase(Suitcase suitcase){
if(suitcase.totalWeight() + minWeight &gt;= maxWeight){
minWeight = minWeight + suitcase.totalWeight();
this.suitcases.add(suitcase);
}
}
public String toString(){
return this.suitcases.size() + &quot; suitcases (&quot; + minWeight + &quot; kg)&quot;;
}
}
public class Suitcase {
private int maxWeight;
private int minWeight;
private ArrayList&lt;Item&gt; items;
public Suitcase(int maxWeight) {
this.minWeight = 0;
this.maxWeight = maxWeight;
this.items = new ArrayList&lt;&gt;();
}
public void addItem(Item item) {
if (minWeight + item.getWeight() &lt;= maxWeight) {
minWeight = minWeight + item.getWeight();
this.items.add(item);
}
}
public void printItems() {
for (Item item : this.items) {
System.out.println(item);
}
}
public Item heaviestItem() {
if (this.items.isEmpty()) {
return null;
}
Item returnObject = this.items.get(0);
for (Item i : this.items) {
if (returnObject.getWeight() &lt; i.getWeight()) {
returnObject = i;
}
}
return returnObject;
}
public int totalWeight() {
return minWeight;
}
public String toString() {
if (this.items.isEmpty()) {
return &quot;no items (0kg)&quot;;
}
if (this.items.size() == 1) {
return this.items.size() + &quot; item (&quot; + minWeight + &quot; kg)&quot;;
}
return this.items.size() + &quot; items (&quot; + minWeight + &quot; kg)&quot;;
}
}
public class Item {
private String name;
private int weight;
public Item(String name, int weight){
this.name = name;
this.weight = weight;
}
public String getName(){
return this.name;
}
public int getWeight(){
return this.weight;
}
public String toString(){
return this.name + &quot; (&quot; + this.weight + &quot; kg)&quot;;
}
}
public class Main {
public static void main(String[] args) {
// You can use the main to test your classes!
Item book = new Item(&quot;Lord of the rings&quot;, 2);
Item phone = new Item(&quot;Nokia 3210&quot;, 1);
Item brick = new Item(&quot;brick&quot;, 4);
Suitcase adasCase = new Suitcase(10);
adasCase.addItem(book);
adasCase.addItem(phone);
Suitcase pekkasCase = new Suitcase(10);
pekkasCase.addItem(brick);
Hold hold = new Hold(1000);
hold.addSuitcase(adasCase);
hold.addSuitcase(pekkasCase);
System.out.println(hold);
}
}

答案1

得分: 1

I haven't read the exercise question just based solely on your code, your problem lies in your condition at hold or specifically at addSuitcases

public void addSuitcase(Suitcase suitcase){
    if(suitcase.totalWeight() + minWeight >= maxWeight){
        minWeight = minWeight + suitcase.totalWeight();
        this.suitcases.add(suitcase);
    }
}

It should be

public void addSuitcase(Suitcase suitcase){
    if(suitcase.totalWeight() + minWeight <= maxWeight){
        minWeight = minWeight + suitcase.totalWeight();
        this.suitcases.add(suitcase);
    }
}

Since you want to add any item that does not overload the suitcase.

Simply put, change ">" to "<=" and you should get

2 suitcases (7 kg)

英文:

I haven't read the exercise question just based solely on your code, your problem lies in your condition at hold or specifically at addSuitcases

public void addSuitcase(Suitcase suitcase){
if(suitcase.totalWeight() + minWeight &gt;= maxWeight){
minWeight = minWeight + suitcase.totalWeight();
this.suitcases.add(suitcase);
}

}

It should be

public void addSuitcase(Suitcase suitcase){
if(suitcase.totalWeight() + minWeight &lt;= maxWeight){
minWeight = minWeight + suitcase.totalWeight();
this.suitcases.add(suitcase);
}

}

Since you want to add any item that does not overload the suitcase.

Simply put, change ">=" to "<=" and you should get

> 2 suitcases (7 kg)

huangapple
  • 本文由 发表于 2020年4月6日 10:00:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/61051924.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定