Java 如何保护阵列不受更改?

Java 如何保护阵列不受更改?,java,Java,我试图从方法fifo和lifo中使用的数组中获取数据。我有主数组,由用户从输入填充。这些数组用于方法fifo和lifo,问题是这两个函数给出了相同的商品价格,因为当fifo使用主数组处理时,它会改变其中的数据。我想防止更改``main array**中的数据,并在两种方法中使用它,而不更改主数组中的数据。有什么想法吗?谢谢 public class Solution { public static int[][] takingGoodsAndPrice(Scanner input, In

我试图从方法
fifo
lifo
中使用的数组中获取数据。我有主数组,由用户输入填充。这些数组用于方法
fifo
lifo
,问题是这两个函数给出了相同的
商品价格
,因为当
fifo
使用主数组处理时,它会改变其中的数据。我想防止更改``main array**中的数据,并在两种方法中使用它,而不更改主数组中的数据。有什么想法吗?谢谢

public class Solution {
    public static int[][] takingGoodsAndPrice(Scanner input, Integer m) {
        final int[][] goodsAndPrice = new int[m][2];

        // Taking goods and its price
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < 2; j++) {
                goodsAndPrice[i][j] = Integer.parseInt(input.next());
            }
        }
        return goodsAndPrice;
    }

    public static int[] takingAmountOfSales(Scanner input, Integer k) {
        final int[] amountOfSales = new int[k];

        // Taking sale of goods
        for (int i = 0; i < k; i++) {
            amountOfSales[i] = Integer.parseInt(input.next());
        }

        return amountOfSales;
    }

    public static Integer fifo(Integer k, Integer m, int[][] goodsAndPrice, int[] amountOfSales) {
        int priceOfRestGoods = 0;
        int[][] goods = goodsAndPrice;
        int[] amount = amountOfSales;

        // Evaluates amount of goods that were not sold
        for (int i = 0; i < k; i++) {
            for (int j = 0; j < m; j++) {
                if (amount[i] == 0)
                    break;

                if (goods[j][0] > amount[i]) {
                    goods[j][0] = goods[j][0] - amount[i];
                    amount[i] = amount[i] - amount[i];
                } else if (goods[j][0] <= amount[i]) {
                    amount[i] = amount[i] - goods[j][0];
                    goods[j][0] = 0;
                }
            }
        }

        // Evaluates price of goods that were not sold
        for (int i = 0; i < m; i++) {
            priceOfRestGoods = priceOfRestGoods + (goods[i][0] * goods[i][1]);
        }

        return priceOfRestGoods;
    }

    public static Integer lifo(Integer k, Integer m, int[][] goodsAndPrice, int[] amountOfSales) {
        int priceOfRestGoods = 0;

        // Evaluates amount of goods that were not sold
        for (int i = 0; i < k; i++) {
            for (int j = m-1; j >= 0; j--) {
                if (amountOfSales[i] == 0)
                    break;

                if (goodsAndPrice[j][0] > amountOfSales[i]) {
                    goodsAndPrice[j][0] = goodsAndPrice[j][0] - amountOfSales[i];
                    amountOfSales[i] = amountOfSales[i] - amountOfSales[i];
                } else if (goodsAndPrice[j][0] <= amountOfSales[i]) {
                    amountOfSales[i] = amountOfSales[i] - goodsAndPrice[j][0];
                    goodsAndPrice[j][0] = 0;
                }
            }
        }

        // Evaluates price of goods that were not sold
        for (int i = 0; i < m; i++) {
            priceOfRestGoods = priceOfRestGoods + (goodsAndPrice[i][0] * goodsAndPrice[i][1]);
        }

        return priceOfRestGoods;
    }
//
//    public static Integer medium() {
//
//    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        final int n = input.nextInt(); // n - total amount of goods
        final int m = input.nextInt(); // m - total amount of goods that has been received
        final int k = input.nextInt(); // k - total amount of goods that has been released

        final int[][] goodsAndPrice = takingGoodsAndPrice(input, m);
        final int[] amountOfSales = takingAmountOfSales(input, k);

        System.out.println(fifo(k, m, goodsAndPrice, amountOfSales));
        System.out.println(lifo(k, m, goodsAndPrice, amountOfSales));
    }
}
公共类解决方案{
公共静态int[]takingGoods和Price(扫描仪输入,整数m){
最终整数[][]goodsAndPrice=新整数[m][2];
//货物及其价格
for(int i=0;i金额[i]){
货物[j][0]=货物[j][0]-金额[i];
金额[i]=金额[i]-金额[i];
}如果(货物[j][0]=0;j--{
if(amountOfSales[i]==0)
打破
如果(goodsAndPrice[j][0]>销售量[i]){
goodsAndPrice[j][0]=goodsAndPrice[j][0]-销售量[i];
amountOfSales[i]=amountOfSales[i]-amountOfSales[i];

}else if(商品价格[j][0]目前,您正在创建新变量来存储数组,但它们将引用这样做的相同数组,这意味着对它们所做的任何更改也将出现在原始参数中。您将希望在这些变量中存储数组的副本。请参阅。

目前,您正在创建新变量来存储数组数组,但它们将引用这样做的相同数组,这意味着对它们所做的任何更改也将出现在原始参数中。您将希望在这些变量中存储数组的副本。执行以下操作时,请参见。

int[][] goods = goodsAndPrice;
您仍在引用
goodsAndPrice
引用的值,即
goods
goodsAndPrice
都将引用相同的值。因此,使用其中一个引用所做的任何更改对于另一个引用都是相同的

您需要做的是创建一份
goodsAndPrice[][]
的副本,并对副本进行更改。您可以按如下方式创建
goodsAndPrice[][]
的副本:

int[][] goods = Arrays.stream(goodsAndPrice).map(int[]::clone).toArray(int[][]::new);

当您执行以下操作时

int[][] goods = goodsAndPrice;
您仍在引用
goodsAndPrice
引用的值,即
goods
goodsAndPrice
都将引用相同的值。因此,使用其中一个引用所做的任何更改对于另一个引用都是相同的

您需要做的是创建一份
goodsAndPrice[][]
的副本,并对副本进行更改。您可以按如下方式创建
goodsAndPrice[][]
的副本:

int[][] goods = Arrays.stream(goodsAndPrice).map(int[]::clone).toArray(int[][]::new);

您可以制作数组的副本并将副本传递给方法
fifo
lifo
。您可以制作数组的副本并将副本传递给方法
fifo
lifo