gpt4 book ai didi

java - 数组只是打印出最后一个元素

转载 作者:行者123 更新时间:2023-12-01 21:45:21 27 4
gpt4 key购买 nike

我遇到了这个错误,它只打印出最后输入的元素,打印它的次数与数组中应该存在的元素相同。

我已经用 System.out.println 对其进行了测试并且正在添加的元素似乎是正确的。如何修复此错误?

package stock.control.system;

import java.util.*;

public class StockArrayList implements StockList {

private ArrayList<StockItem> StockItems;
private int index = 0;
private int update;
private int counter = 0;

public StockArrayList() {
StockItems = new ArrayList<StockItem>(counter);
}

@Override
public void addItem(StockItem item) {
StockItem aItem = new StockItem(StockItem.getItemID(),
StockItem.getItemDesc(),
StockItem.getPrice(),
StockItem.getQuantity(),
StockItem.getReOrderLevel());

StockItems.add(counter, aItem);
counter++;
}

@Override
public String formatStockList(){

String temp = StockItem.format();

for (StockItem items : StockItems) {
temp = temp + items.arrayFormat() + "\n";
}
return temp;
}
}

主要方法:

public class StockArrayListTester {

public static void main(String[] args) {
StockArrayList Stock = new StockArrayList();
Stock.addItem(new StockItem("P123","1TB Hard drive",75.00,267,50));
Stock.addItem(new StockItem("P125","i7 6800HQ Processor",257.00,113,45));
Stock.addItem(new StockItem("P129","i5 500HQ Processor",127.00,10,45));
Stock.deleteItem("P129");
System.out.printf(Stock.formatStockList());

}

}

库存商品类别

package stock.control.system;

import java.util.*;

public class StockItem {
private static String itemID; // Five alpha-numeric characters
private static String itemDesc; // Item description
private static double price; // Item price in pounds sterling
private static int quantity; // Quantity in stock
private static int reOrderLevel; // Level at which to re-order

public StockItem(String itemID, String itemDesc, double price, int quantity, int reOrderLevel) {
this.itemID = itemID;
this.itemDesc = itemDesc;
this.price = price;
this.quantity = quantity;
this.reOrderLevel = reOrderLevel;
}

@Override
public String toString() {
String toString ="[Item ID = " + this.itemID + ", Item Description = " +
this.itemDesc + ", Price = " + this.price + ", Quantity = " +
this.quantity + ", Re Order Level = " + this.reOrderLevel + "]";

return toString;
}

public static String format() {
String format = " STOCK ITEMS"
+ String.format("\n%-10s%-30s%-10s%-12s%-14s%-10s%-30s%-10s%-12s%-14s\n",
"ItemID","Item Description",
"Price","Quantity", "Re Order Level", "\n******",
" ****************"," *****", " ********",
" **************");
return format;
}

public String arrayFormat() {
return String.format("%-10s%-30s%-10s%-12s%-14s",
StockItem.getItemID(),
StockItem.getItemDesc(),
StockItem.getPrice(),
StockItem.getQuantity(),
StockItem.getReOrderLevel());
}

public static String getItemID(){
return itemID;
}

public static String getItemDesc() {
return itemDesc;
}

public static double getPrice() {
return price;
}

public double setPrice(double price) {
this.price = price;
return price;
}

public static int getQuantity() {
return quantity;
}

public int setQuantity(int quantity) {
this.quantity = quantity;
return quantity;
}

public static int getReOrderLevel(){
return reOrderLevel;
}

public int setReOrderLevel(int reOrderLevel){
this.reOrderLevel = reOrderLevel;
return reOrderLevel;
}
}

我得到的输出是:

STOCK ITEMS
ItemID Item Description Price Quantity Re Order
P129 i5 500HQ Processor 127.0 10 45
P129 i5 500HQ Processor 127.0 10 45
P129 i5 500HQ Processor 127.0 10 45
BUILD SUCCESSFUL (total time: 0 seconds)

最佳答案

通常,切勿在构造函数中设置静态字段。这几乎肯定是一个错误。恕我直言,这应该是一个编译器错误,但事实并非如此。

在这种情况下,您希望 StockItem 的每个实例都不同,但是通过将字段设置为static,您可以确保只有一个副本,只有一个值对于那些领域。我建议您将它们设为实例字段。

public class StockItem {
private final String itemID; // Five alpha-numeric characters
private final String itemDesc; // Item description
private double price; // Item price in pounds sterling
private int quantity; // Quantity in stock
private int reOrderLevel; // Level at which to re-order

public StockItem(String itemID, String itemDesc, double price, int quantity, int reOrderLevel) {
this.itemID = itemID;
this.itemDesc = itemDesc;
this.price = price;
this.quantity = quantity;
this.reOrderLevel = reOrderLevel;
}

关于java - 数组只是打印出最后一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36060554/

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