Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何访问此数组的itemPrice部分?_Java_Arrays - Fatal编程技术网

Java 如何访问此数组的itemPrice部分?

Java 如何访问此数组的itemPrice部分?,java,arrays,Java,Arrays,所以我需要获取索引的itemPrice部分并将它们全部添加到一起,但我不确定如何访问它。我是否可以使用GroceryItemOrder类中的getCost方法并将其连续添加到GroceryList类中的totalCost中,或者我是否需要访问每个存储对象的itemPrice和quantity部分 public class GroceryList { public GroceryItemOrder[] groceryList = new GroceryItemOrder[0];

所以我需要获取索引的itemPrice部分并将它们全部添加到一起,但我不确定如何访问它。我是否可以使用GroceryItemOrder类中的getCost方法并将其连续添加到GroceryList类中的totalCost中,或者我是否需要访问每个存储对象的itemPrice和quantity部分

public class GroceryList {

    public GroceryItemOrder[] groceryList = new GroceryItemOrder[0];
    public int manyItems;


    public GroceryList() {
        final int  INITIAL_CAPACITY = 10;
        groceryList = new GroceryItemOrder[INITIAL_CAPACITY];
        manyItems = 0;
    }

    //Constructs a new empty grocery list array
    public GroceryList(int numItem) {
        if (numItem < 0)
            throw new IllegalArgumentException
                    ("The amount of items you wanted to add your grocery list is negative: " + numItem);
        groceryList = new GroceryItemOrder[numItem];
        manyItems = 0;
    }

    public void add(GroceryItemOrder item) {
        if (manyItems <= 10) {
            groceryList[manyItems] = item;
        }
        manyItems++;
    }

    //
    // @return the total sum list of all grocery items in the list
    public double getTotalCost() {
        double totalCost = 0;
        for (int i = 0; i < groceryList.length; i++ ) {
            //THIS PART
        }
        return totalCost;
    }

}

谢谢你的回复!我让它工作起来,并了解现在这里发生了什么。

您首先需要访问数组中的
GroceryItemOrder
实例,然后从那里访问其
itemPrice
字段,如下所示

groceryList[0].itemPrice
将为您提供groceryList数组中第一个GroceryStorder的itemPrice。如果您想使用方法来执行此操作,请在
grocerylistoreder
类中添加
getItemPrice
方法

public getItemPrice() {
    return itemPrice;
}
然后您可以访问数组中每个杂货店店主的itemPrice,如下所示

groceryList[0].getItemPrice()
将执行与杂货店列表[0]相同的操作。itemPrice。如果要获得
groceryList
数组中所有对象的总成本,请使用
getcost
方法,使用循环将所有
itemPrice
字段乘以
itemQuantity
字段(因为它是每个对象的总成本的总和)

double totalCost = 0;
for (int i = 0; i < groceryList.length; i++) {
    totalCost += groceryList[i].getcost();
}
double totalCost=0;
for(int i=0;i
首先需要访问数组中的
GroceryItemOrder
实例,然后从那里访问其
itemPrice
字段,如下所示:

groceryList[0].itemPrice
将为您提供groceryList数组中第一个GroceryStorder的itemPrice。如果您想使用方法来执行此操作,请在
grocerylistoreder
类中添加
getItemPrice
方法

public getItemPrice() {
    return itemPrice;
}
然后您可以访问数组中每个杂货店店主的itemPrice,如下所示

groceryList[0].getItemPrice()
将执行与杂货店列表[0]相同的操作。itemPrice。如果要获得
groceryList
数组中所有对象的总成本,请使用
getcost
方法,使用循环将所有
itemPrice
字段乘以
itemQuantity
字段(因为它是每个对象的总成本的总和)

double totalCost = 0;
for (int i = 0; i < groceryList.length; i++) {
    totalCost += groceryList[i].getcost();
}
double totalCost=0;
for(int i=0;i
首先,您应该封装
GroceryItemOrder
类的所有字段,因此所有字段都应该是该类的私有成员,然后使用它们的setter/getter方法在
GroceryList
中访问它们

其次,这个实现有一个bug。第二个构造函数获取
numItem
作为输入,并相应地初始化数组大小。但是,
add
方法不查看实际大小,这可能会导致无效数组索引异常。考虑这个代码:

GroceryList list = new GroceryList(2);
for (int i=0; i<10; i++)
    list.add(new GroceryItemOrder("grocery", 5, 10));
GroceryList list=新的GroceryList(2);

对于(int i=0;i首先,您应该封装
GroceryItemOrder
类的所有字段,因此所有字段都应该是该类的私有成员,然后使用它们的setter/getter方法在
GroceryList
中访问它们

第二个构造函数得到<代码> NUMATION/COD>作为输入,并初始化数组大小。但是,<代码> Addiaby/Cult>方法不考虑实际大小,这可能会导致无效的数组索引异常。请考虑此代码:

GroceryList list = new GroceryList(2);
for (int i=0; i<10; i++)
    list.add(new GroceryItemOrder("grocery", 5, 10));
GroceryList list=新的GroceryList(2);

对于(int i=0;i这对我有效,您需要设置
static GroceryItemOrder[]groceryList=new GroceryItemOrder[0];

//
// @return the total sum list of all grocery items in the list
public static double getTotalCost() {
    double totalCost = 0;
    for (int i = 0; i < groceryList.length; i++ )
    {
        totalCost += groceryList[i].getcost();
    }
    return totalCost;
}
//
//@返回列表中所有杂货项目的总和列表
公共静态双getTotalCost(){
双重总成本=0;
for(int i=0;i
这对我有效,您需要设置
静态GroceryItemOrder[]groceryList=new GroceryItemOrder[0];

//
// @return the total sum list of all grocery items in the list
public static double getTotalCost() {
    double totalCost = 0;
    for (int i = 0; i < groceryList.length; i++ )
    {
        totalCost += groceryList[i].getcost();
    }
    return totalCost;
}
//
//@返回列表中所有杂货项目的总和列表
公共静态双getTotalCost(){
双重总成本=0;
for(int i=0;i
由于
GroceryItemOrder
的实例变量具有
public
可见性,您不需要为其使用方法。只需使用
GroceryItemOrder.itemPrice
访问它们即可。由于
GroceryItemOrder
的实例变量具有
public
可见性,您不需要为其使用方法。Sim请使用
GroceryItemOrder.itemPrice
访问它们。