1
[[-6, 3, 9], [-7, 2, 9], [-3, 2, 5], ... , [3, 4, 1]]

The array that I'm using is structured like the one above.

My goal is to divide this array based upon a certain position that has been previously determined.

I've attempted Arrays.copyOf , Arrays.copyOfRange, and System.arraycopy - but have not experienced success, which is why I wrote my own method for this; it also didn't work.

partitionResult is an instance (variable) array of type int structured just like arrayOfVals

arrayOfVals seems to become initialized with the entire partitionResult array despite my intention of only copying only a portion. I have tested i.e. System.out.println (partitionResult[begin+i][j]) , and the values printed are as desired.

 private int[][] copyArray(int begin, int end)
    {
        int SUBARRAY_SIZE = 2;
        // below the '+1' is due to zero-indexing
        int[][] arrayOfVals = new int[end-begin+1][SUBARRAY_SIZE+1];
        end -= begin;

        for (int i = 0; i <= end; i++) {
            for (int j = 0; j <= SUBARRAY_SIZE; j++) {
                arrayOfVals[begin][j] = partitionResult[begin+i][j];
            }
        }
        return arrayOfVals;
    }

Why can I not do the following as desired?

private void foo(int begin)
{
    int[][] arrayOne = copyArray(0, begin);
    int[][] arrayTwo = copyArray(begin+1, partitionResult.length -1);
    ...

}

Edit:

[[-6, 3, 9], [-7, 2, 9], [-3, 2, 5], [3, 4, 1], [0, 5, 5], [2, 3, 1], [3, 4, 1]]

This is my test array. I would like to split this array using the copyArray method at the defined position begin.

When I print the values that I'd like copied, partitionResult[begin+i][j], the result is exactly as it should be; however, display the final arrayOfVals - the output is not what I printed, it is the entire partitionResult array.

I want arrayOne to equal [[-6, 3, 9], [-7, 2, 9], [-3, 2, 5]]

and arrayTwo to equal [[3, 4, 1], [0, 5, 5], [2, 3, 1], [3, 4, 1]]

Edit2: The problem was not with the method copyArray but with another method. The toString method that I wrote was displaying the values used by the instance variable partitionResult rather than the array that I passed to it - this made it seem as if nothing was being copied. The mistake should have been obvious to me. I greatly appreciate the advice.

Though, one small bug was found by @Andrea.

4

4 回答 4

1

这应该很简单,您只是通过 mutatingend使自己变得困难,从而难以理解循环的进程。只需复制beginend(包括)之间的值,但请确保克隆每个子数组。(克隆有效地替换了您的内部循环。)

private int[][] copyArray(int begin, int end) {
    // Calculate the size of the output
    // below the '+1' is due to zero-indexing
    int size = end - begin + 1;
    int[][] arrayOfVals = new int[size][];
    for (int i = 0; i < size; i++) {
        // Clone each subarray to make sure changes to the copy
        // don't affect the internal array
        // (A shallow .clone() suffices for integer arrays)
        arrayOfVals[i] = partitionResult[begin + i].clone();
    }
    return arrayOfVals;
}

这会在调用时为您的示例输入提供预期的输出foo(2)

于 2013-09-05T15:56:36.957 回答
1

错误应该在

 arrayOfVals[begin][j] = partitionResult[begin+i][j];

将其更改为

 arrayOfVals[i][j] = partitionResult[begin+i][j];

因为您新创建的数组必须从 0 开始插入值。

于 2013-09-05T15:46:53.050 回答
0

如果 fromArray 是输入数组并且 index 是您想要中断输入数组的索引,您可以这样做:

    System.arraycopy(fromArray, 0, arrayOne, 0, index);
    System.arraycopy(fromArray, index+1, arrayTwo, 0, fromArray.length-index);
于 2013-09-05T15:49:49.137 回答
0

当你创建一个二维数组时,你所拥有的是一个数组数组。看看这样的问题:

[0,0]  [1,0]  [2,0] ... [n,0]
[0,1]  [1,1]  [2,1] ... [n,1]
[0,2]  [1,2]  [2,2] ... [n,2]  

[[-6, 3, 9], [-7, 2, 9], [-3, 2, 5], ... , [3, 4, 1]]

我们可以如下表示您的示例数据集:

[-6]  [-7]  [-3] ... [ 3]
[ 3]  [ 2]  [ 2] ... [ 4]
[ 9]  [ 9]  [ 5] ... [ 1]
于 2013-09-05T16:04:13.520 回答