Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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 添加第3项后临时阵列不工作_Java_Arrays - Fatal编程技术网

Java 添加第3项后临时阵列不工作

Java 添加第3项后临时阵列不工作,java,arrays,Java,Arrays,如果用户决定继续向购物车添加物品,我必须制作一个Temp数组,以不断调整数组列表的大小,但是我的Temp数组在我尝试向购物车添加3个不同物品之前一直有效 我被指示这样做,而不是一个数组列表,以显示数组的难度 orderProduct [productCount] = aProduct; orderQuantity [productCount] = aQuantity; } } 当购物车中已有产品时,您忘记增加productCount 此外,您可以将产品和数量数组设置

如果用户决定继续向购物车添加物品,我必须制作一个
Temp数组
,以不断调整数组列表的大小,但是我的
Temp数组
在我尝试向购物车添加3个不同物品之前一直有效

我被指示这样做,而不是一个
数组列表
,以显示数组的难度

     orderProduct [productCount] = aProduct;
     orderQuantity [productCount] = aQuantity;
    }
}

当购物车中已有产品时,您忘记增加
productCount

此外,您可以将产品和数量数组设置为临时数组,而不是复制回临时数组

orderProduct = tempOrderedProducts;
orderQuantity = tempOrderedQuantity;

因为您在调整数组大小后忘记了
productCount++
。 以下代码将起作用:

public void setOrderProduct(Product aProduct, int aQuantity) {

    if (productCount == 0) {
        orderProduct[0] = aProduct;
        orderQuantity[0] = aQuantity;

    } else {
        Product[] tempOrderedProducts = new Product[orderProduct.length + 1];
        int[] tempOrderedQuantity = new int[orderQuantity.length + 1];
        for (int i = 0; i < orderProduct.length; i++) {
            tempOrderedProducts[i] = orderProduct[i];
            tempOrderedQuantity[i] = orderQuantity[i];
        }
        orderProduct = new Product[tempOrderedProducts.length];
        orderQuantity = new int[tempOrderedQuantity.length];
        for (int i = 0; i < orderQuantity.length; i++) {
            orderProduct[i] = tempOrderedProducts[i];
            orderQuantity[i] = tempOrderedQuantity[i];
        }
        orderProduct[productCount] = aProduct;
        orderQuantity[productCount] = aQuantity;

        productCount++; //you forgot this
    }
}
public void setOrderProduct(Product aProduct, int aQuantity) {

    if (productCount == 0) {
        orderProduct[0] = aProduct;
        orderQuantity[0] = aQuantity;

    } else {

        Product[] tempOrderedProducts = new Product[orderProduct.length + 1];
        int[] tempOrderedQuantity = new int[orderQuantity.length + 1];

        //System.arraycopy is more convenient and efficient
        System.arraycopy(orderProduct, 0, tempOrderedProducts, 0, orderProduct.length);
        System.arraycopy(orderQuantity, 0, tempOrderedQuantity, 0, orderQuantity.length);

        //you don't need to copy back, just re-assign the reference 
        orderProduct = tempOrderedProducts;
        orderQuantity = tempOrderedQuantity;

        orderProduct[productCount] = aProduct;
        orderQuantity[productCount] = aQuantity;

        productCount++;
    }
}