我正在使用Netbeans,并且编写了一种方法,该方法未完全执行应做的工作:

private ArrayList<String[]>ProductsInStock;

public void DisplayStock() {
    ArrayList<String[]> Sort = new ArrayList<String[]>();
    System.out.println("");
    for (int i = 0; i < ProductsInStock.size(); i++) {
        if (ProductsInStock.get(i)[2].equals(Products.get(ProductCodeCB.getSelectedIndex())[1])) {
            boolean foundColor = false;
            int size = Sort.size();//Since the size will differ dynamically
            for (int k = 0; k < size; k++) {
                if (Sort.get(k)[3].equals(ProductsInStock.get(i)[3])) {
                    foundColor = true;
                    if (Sort.get(k)[4].equals(ProductsInStock.get(i)[4])) {
                        String S[] = Sort.get(k);
                        S[5] = (Integer.parseInt(Sort.get(k)[5]) + Integer.parseInt(ProductsInStock.get(i)[5])) + "";
                        Sort.set(k, S);
                        break;
                    }
                    if (k == Sort.size() - 1) {
                        Sort.add(ProductsInStock.get(i));
                    }
                } else if (foundColor == true) {
                    Sort.add(k, ProductsInStock.get(i));
                    break;
                }
            }
            System.out.print(ProductsInStock.get(0)[5]+" ");
            if (foundColor == false) {
                Sort.add(ProductsInStock.get(i));
            }
        }
    }
}
}


该方法不应更改ProductsInStock.get(0)[5]的值,但是每次调用该方法时它都会将其递增1,我放置了“ System.out.println()”以向您展示价值实际上正在改变。结果如下:1 1 1 1 1 1 1 1 2 2
当我添加“ S [5] = ProductsInStock.get(i)[5];”行时,结果更改为:1 1 1 1 1 1 1 1 1 1(应为:)

public void DisplayStock() {
    ArrayList<String[]> Sort = new ArrayList<String[]>();
    System.out.println("");
    for (int i = 0; i < ProductsInStock.size(); i++) {
        if (ProductsInStock.get(i)[2].equals(Products.get(ProductCodeCB.getSelectedIndex())[1])) {
            boolean foundColor = false;
            int size = Sort.size();//Since the size will differ dynamically
            for (int k = 0; k < size; k++) {
                if (Sort.get(k)[3].equals(ProductsInStock.get(i)[3])) {
                    foundColor = true;
                    if (Sort.get(k)[4].equals(ProductsInStock.get(i)[4])) {
                        String S[] = Sort.get(k);
                        S[5] = (Integer.parseInt(Sort.get(k)[5]) + Integer.parseInt(ProductsInStock.get(i)[5])) + "";
                        Sort.set(k, S);
                        S[5]=ProductsInStock.get(i)[5]; //<<<<HERE>>>>
                        break;
                    }
                    if (k == Sort.size() - 1) {
                        Sort.add(ProductsInStock.get(i));
                    }
                } else if (foundColor == true) {
                    Sort.add(k, ProductsInStock.get(i));
                    break;
                }
            }
            System.out.print(ProductsInStock.get(0)[5]+" ");
            if (foundColor == false) {
                Sort.add(ProductsInStock.get(i));
            }
        }
    }
}


如您所见,没有一个“ ProductsInStock.set()”或“ ProductsInStock.get()[] =”可以更改数组列表中的任何值。

最佳答案

当你写这个:

 Sort.add(ProductsInStock.get(i));


您正在将ProductsInStock.get(i)数组的引用添加到Sort列表。 Sort.get(Sort.size()-1)中所做的任何更改都会影响原始数组。

因此代码如

String S[] = Sort.get(k);
S[5] = ...


修改ProductsInStock列表的数组之一。

为了避免这种情况,您应该先创建数组的副本,然后再将其添加到另一个List中:

Sort.add(Arrays.copyOf(ProductsInStock.get(i),ProductsInStock.get(i).length));

07-24 09:18