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

时间:2016-03-17 12:27:17

标签: java arrays

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

我已使用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)

3 个答案:

答案 0 :(得分:3)

通常,永远不要在构造函数中设置static个字段。这几乎肯定是一个错误。恕我直言,这应该是一个编译错误,但事实并非如此。

在这种情况下,您希望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;
    }

答案 1 :(得分:0)

我已经有一段时间了,因为我已经使用过Java,但是在你的StockList类的addItem()方法中,你传递了一个参数&#39; item&#39;但是从不在方法中使用它。

你为什么试图&#34;得到&#34;当您将它们作为StockItem对象传递给函数时要添加的库存项目的所有属性?

答案 2 :(得分:0)

猜猜这里有问题:

@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++;
}

所有这些getter都是静态方法。这对我来说没有意义,因为我认为你想要获得属于不同对象的实例变量。您必须使用打印出的值初始化StockItem类实例变量,否则我认为您的代码甚至不会编译。

无论如何为什么不将作为参数传递的项目直接添加到库存清单中? 像这样:

@Override
public void addItem(StockItem item) {

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