Java数组被下一个元素/值覆盖(方法,数组)

时间:2014-12-13 07:13:30

标签: java arrays methods

我在发帖之前已经阅读了几个帖子,但是我读过的这些帖子似乎比我的理解复杂得多。

以下是我的代码,

// InventoryApp(主要)

package myInventoryPkg;

public class InventoryApp 
{
    public static void main(String[] args) 
    {
        Inventory myInventory = new Inventory(100);
        Product myProduct = new Product();

        myProduct.setName("Pen");
        myProduct.setPrice(1.25);
        myProduct.setQuantity(50);
        myInventory.addProduct(myProduct);

        //System.out.println("Product    : " + myInventory.items[0].getName());
        //System.out.println("Current Size : " + myInventory.getCurrentSize()); //Size 1.
        //I am able to show "Pen" by printing the codes above when pointing to item[0]


        myProduct.setName("Paper");
        myProduct.setQuantity(500);
        myProduct.setPrice(12.85);
        myInventory.addProduct(myProduct);

        //System.out.println("Product    : " + myInventory.items[1].getName());
        //System.out.println("Current Size : " + myInventory.getCurrentSize()); //Size 2.
        //I am NOT able to show "Pen" by printing the codes above when pointing to item[0]
        //instead, whether I point to item[0] or item[1], the system shows me "Paper", which
        //should not be the case...

        /* (commented it due to the above error)
        for(int i = 0; i < myInventory.items.length; i++)
        {
            System.out.println("Product  : " + myInventory.items[i].getName());
            System.out.println("Price    : " + myInventory.items[i].getPrice());
            System.out.println("Quantity : " + myInventory.items[i].getQuantity());
            System.out.println("--------------------------------");
        }
        */
    }

}

//库存(方法)

package myInventoryPkg;

public class Inventory 
{
    Product[] items;
    private int currentSize;

    public Inventory(int inputMaxSize)
    {
        currentSize = 0;
        items = new Product[inputMaxSize];
    }

public boolean addProduct(Product inputProduct)
    {
        if(currentSize < items.length)
        {
            items[currentSize] = inputProduct; 
            currentSize++;
            return true;
        }
        else
        {
            return false;
        }   
    }

    public int getCurrentSize()
    {
        return currentSize;
    }

    public Product getProduct(int index)
    {
        return items[index];
    }
}

//产品(方法)

package myInventoryPkg;

public class Product 
{
    private String name;
    private int quantity;
    private double price;

    public Product()
    {
        String inputName = "";
        int inputQuantity = 0;
        double inputPrice = 0.0;

        name = inputName;
        quantity = inputQuantity;
        price = inputPrice;
    }

    public String getName()
    {
        return name;
    }

    public int getQuantity()
    {
        return quantity;
    }

    public double getPrice()
    {
        return price;
    }

    public void setName(String inputName)
    {
        name = inputName;
    }

    public void setQuantity(int inputQuantity)
    {
        quantity = inputQuantity;
    }

    public void setPrice(double inputPrice)
    {
        price = inputPrice;
    }
}

- 我感谢任何帮助,我的所有代码都已添加。 请注意,在运行此应用程序时,除了数组向我显示的错误值外,我没有收到错误,这似乎是我最大的问题。

此致 玉斌

-

线程关闭。

3 个答案:

答案 0 :(得分:0)

您需要实例化一个新的Product。您只创建了一个Product并继续修改该单数产品

Product myProduct = new Product(); // <-- a reference to a product.
myProduct.setName("Pen");
myProduct.setPrice(1.25);
myProduct.setQuantity(50);
myInventory.addProduct(myProduct); // <-- a copy of the reference.

myProduct = new Product(); // <-- create a new product.
// now you have a second product....
myProduct.setName("Paper");
myProduct.setQuantity(500);
myProduct.setPrice(12.85);
myInventory.addProduct(myProduct);

答案 1 :(得分:0)

您一次又一次地将相同的产品对象添加到列表中。因此,列表最终会引用一个唯一的Product对象,其状态是最后一个。如果您需要多种不同的产品,则需要多次致电new Product()

答案 2 :(得分:0)

您没有创建Product类的新实例,因此每次调用setSomething时,您只是更改现有实例,当您将该实例添加到数组中的新插槽时,您只是创建一个新别名。所有项目都指向内存中的相同项目。