gpt4 book ai didi

java - 数组的 toString 方法

转载 作者:行者123 更新时间:2023-12-04 10:15:25 25 4
gpt4 key购买 nike

我正在努力完成一项实际上非常简单的任务,即从项目数组中打印出项目,例如:

arr[3,4,2,5]

0 件 (0 公斤)

0 件 (1 公斤)

0 件 (2 公斤)

等等。
这就是我所做的,但我的程序不会打印任何东西:(请帮帮我。

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

public class Suitcase {
private ArrayList<Item> items = new ArrayList<>();
private final int maxWeight;
private int[] arr = new int[items.size()];

public Suitcase(int maxWeight) {
this.maxWeight = maxWeight;
}

public void addItem(Item item) {
int itemsWeight = 0;
for(Item i: items){
itemsWeight += i.getWeight();
}
if(itemsWeight + item.getWeight() <= this.maxWeight){
items.add(item);
}
}

public void array() {
for(Item item: items){
int index = item.getWeight();
this.arr[index] += 1;
}
}

public String toString() {
String returnValue = "";
for(int i = 0; i < arr.length; i++){
returnValue = arr[i] + " items " + i + " kg";
}
return returnValue;
}
}

public class Item {
private int weight;
private String name;

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 + "(" + String.valueOf(this.weight) + ")";
}
}

这是我的主要类(class),但它不会打印任何内容:
public class Main {
public static void main(String[] args) {
Item book = new Item("Lord of the rings", 2);
Item phone = new Item("Nokia 3210", 1);
Item brick = new Item("brick", 4);

Suitcase suitcase = new Suitcase(5);
System.out.println(suitcase.toString());

suitcase.addItem(book);
System.out.println(suitcase);

suitcase.addItem(phone);
System.out.println(suitcase);

suitcase.addItem(brick);
System.out.println(suitcase);
}
}

最佳答案

备注:

  • 您无需调用suitcase.toString()在打印 suitcase 时目的。当System.out.println(suitcase); is 隐式转换为 System.out.println(suitcase.toString()); .
  • 您可以通过一个变量来跟踪手提箱中的总重量,从而使您的设计更简单。另外,在 Item 中创建一个变量跟踪行李箱中元素的数量。
  • 你不需要int[] arr .它只是增加了不必要的复杂性。去掉它。
  • 最好使用enhanced for loop如果可以的话。

  • 下面给出的是包含上述几点的代码:
    import java.util.ArrayList;

    class Suitcase {

    private ArrayList<Item> items = new ArrayList<>();
    private final int maxWeight;
    private int totalWeight;

    public Suitcase(int maxWeight) {
    this.maxWeight = maxWeight;
    }

    public void addItem(Item item) {
    if (totalWeight + item.getWeight() <= maxWeight) {
    int index = items.indexOf(item);
    if (index == -1) {// It means the item does not exist in the suitcase
    items.add(item);
    }
    // If the item already exists, do not add it's entry again; just update its
    // count and the totalWeight of the suitcase
    totalWeight += item.getWeight();
    item.setCount(item.getCount() + 1);
    System.out.println(item.getName() + " was added in the suitcase");
    } else {
    System.out.println(item.getName() + " can not be accommodated in the suitcase.");
    }
    }

    public String toString() {
    String returnValue = "";
    for (Item item : items) {
    returnValue += "No. of " + item.getName() + " in the suitcase = " + item.getCount()
    + ", its total weight = " + item.getCount() * item.getWeight() + "kg\n";
    }
    if (returnValue.isEmpty()) {
    returnValue = "The suitcase is empty.";
    } else {
    returnValue += "Total weight of the suitcase = " + totalWeight + "kg";
    }
    return returnValue;
    }
    }

    class Item {
    private int weight;
    private String name;
    private int count;

    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 int getCount() {
    return count;
    }

    public void setCount(int count) {
    this.count = count;
    }

    @Override
    public String toString() {
    return "Item [weight=" + weight + ", name=" + name + ", count=" + count + "]";
    }
    }

    public class Main {
    public static void main(String[] args) {
    Item book = new Item("Lord of the rings", 2);
    Item phone = new Item("Nokia 3210", 1);
    Item brick = new Item("brick", 4);

    Suitcase suitcase = new Suitcase(5);
    System.out.println(suitcase);

    suitcase.addItem(book);
    suitcase.addItem(phone);
    suitcase.addItem(brick);
    suitcase.addItem(phone);
    suitcase.addItem(book);
    System.out.println(suitcase);
    }
    }

    输出:
    The suitcase is empty.
    Lord of the rings was added in the suitcase
    Nokia 3210 was added in the suitcase
    brick can not be accommodated in the suitcase.
    Nokia 3210 was added in the suitcase
    Lord of the rings can not be accommodated in the suitcase.
    No. of Lord of the rings in the suitcase = 1, its total weight = 2kg
    No. of Nokia 3210 in the suitcase = 2, its total weight = 2kg
    Total weight of the suitcase = 4kg

    如有任何疑问/问题,请随时发表评论。

    关于java - 数组的 toString 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61084157/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com