Java 将对象添加到指定的对象数组

Java 将对象添加到指定的对象数组,java,arrays,object,methods,constructor,Java,Arrays,Object,Methods,Constructor,我试图在java中向InvTrans数组“widget”添加一个Inv对象。我在InvTrans类中有一个私有数组的构造函数,在Invt类中有一个Inv实例的构造函数,还有一个接受InvTrans对象数组,它将接受Inv实例(我想我使用的是正确的词汇表)。我的困境是,我试图弄清楚是否要将其添加到数组中,但我很难显示数组中的内容 public class InvTrans { // variable for a counter in InvTrans Obj. private i

我试图在java中向InvTrans数组“widget”添加一个Inv对象。我在InvTrans类中有一个私有数组的构造函数,在Invt类中有一个Inv实例的构造函数,还有一个接受InvTrans对象数组,它将接受Inv实例(我想我使用的是正确的词汇表)。我的困境是,我试图弄清楚是否要将其添加到数组中,但我很难显示数组中的内容

public class InvTrans

{
    // variable for a counter in InvTrans Obj.
    private int InvTransIndex = 0;
    private Inv[] transactionArray;

    // Constructor for InvTrans array.
    public InvTrans() {
        this.transactionArray = new Inv[100];
    }

    public static void main(String args[]) {

        InvTrans widget = new InvTrans();

        Inv order1 = new Inv("March 5th, 2016", "Received", 20, 0001, 2.10, 2.20);
        Inv order2 = new Inv("March 6th, 2016", "Received", 100, 0002, 2.10, 2.50);
        Inv order3 = new Inv("March 7th, 2016", "Received", 100, 0003, 2.10, 2.50);
        Inv order4 = new Inv("March 12th, 2016", "Sold", 140, 0004, 2.40, 2.60);

        widget.addLine(order1);
        order1.display();
    }

    public void addLine(Inv a) {
        transactionArray[InvTransIndex] = a;
        InvTransIndex++;
    }

    // Method to add an inventory object to the array.
    public boolean setTransactionLine(Inv i) {
        transactionArray[InvTransIndex] = i;
        InvTransIndex = InvTransIndex + 1;
        return true;
    }
}

// Class Inv (Inventory) contains a constructor for a part and a display method.
class Inv {
    /*
     * Need a constructor which will hold the fields necessary for our Inventory
     * Tracker. Need: a) Date - Date transaction occurred. b) Units - Number of
     * items added or subtracted from inventory. c) Type - Description of
     * transaction. Sale, Receipt, Adjustment. d) Reference Number. e) Cost per
     * Unit - price paid for each unit in inventory. Unused sales. f) Price per
     * unit - What each unit was sold for. Unused receipts.
     */
    String date, type;
    int units, referenceNumber;
    double costPerUnit, pricePerUnit;

    Inv(String d, String t, int u, int r, double c, double p) {
        date = d;
        type = t;
        units = u;
        referenceNumber = r;
        costPerUnit = c;
        pricePerUnit = p;
    }

    public void display() {
        System.out.println(this.date + "\t" + this.type + "\t" + this.units + "\t\t\t\t\t" + this.referenceNumber
                + "\t\t\t\t\t" + this.costPerUnit + "\t\t\t" + this.pricePerUnit);
    }
}

这就是列表有用的地方。在Java中使用的列表主要有两种类型:
ArrayList
LinkedList

我不会详细讨论这些差异,但对于您来说,因为您将使用堆栈数据结构,所以ArrayList是完美的,因为在幕后,它与您试图编写的代码完全相同;增加数组的大小,并为要添加的对象设置最后一个索引

如何使用它:

而不是创建
Inv[]
数组。这样做:
ArrayList事务=新建ArrayList()

现在,添加事务很容易,只需执行以下操作:
transactions.add(order1)

好了


要访问列表条目,可以使用
transactions.get(int-index)
并且您也可以像数组一样在foreach循环中使用它们。

您使用的是什么语言?你的问题是什么?有些东西不起作用了吗?没有编译?与您期望的代码相比,上面的代码出了什么问题?它可以编译,但我已经详细介绍了如何检查是否向对象数组中添加了某些内容。您可以在数组中循环查看Inv对象并输出其内容,以查看其中包含的内容。只需公开一个方法来调试它。讲师指定使用数组,因为在后面的作业中,我将实现一个堆栈数据结构,以将日期信息推送到数组中。