英文:
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<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() + " suitcases (" + minWeight + " kg)";
}
}
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 "no items (0kg)";
}
if (this.items.size() == 1) {
return this.items.size() + " item (" + minWeight + " kg)";
}
return this.items.size() + " items (" + minWeight + " kg)";
}
}
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 + " kg)";
}
}
public class Main {
public static void main(String[] args) {
// You can use the main to test your classes!
Item book = new Item("Lord of the rings", 2);
Item phone = new Item("Nokia 3210", 1);
Item brick = new Item("brick", 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 >= 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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论