简体   繁体   English

Java中数组的部分副本

[英]Partial copy of an array in java

[[-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; 我已经尝试过Arrays.copyOfArrays.copyOfRangeSystem.arraycopy ,但是没有取得成功,这就是为什么我为此编写了自己的方法的原因; it also didn't work. 它也没有用。

partitionResult is an instance (variable) array of type int structured just like arrayOfVals partitionResult是一个int类型的实例(变量)数组,其结构类似于arrayOfVals

arrayOfVals seems to become initialized with the entire partitionResult array despite my intention of only copying only a portion. 尽管我只打算复制一部分,但arrayOfVals似乎已用整个partitionResult数组初始化。 I have tested ie System.out.println (partitionResult[begin+i][j]) , and the values printed are as desired. 我已经测试了System.out.println (partitionResult[begin+i][j]) ,并且打印的值是所需的。

 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 . 我想在begin位置使用copyArray方法拆分此数组。

When I print the values that I'd like copied, partitionResult[begin+i][j] , the result is exactly as it should be; 当我打印要复制的值partitionResult[begin+i][j] ,结果与应有的结果完全相同。 however, display the final arrayOfVals - the output is not what I printed, it is the entire partitionResult array. 但是,显示最终的arrayOfVals输出不是我打印的内容,而是整个partitionResult数组。

I want arrayOne to equal [[-6, 3, 9], [-7, 2, 9], [-3, 2, 5]] 我想要arrayOne等于[[-6, 3, 9], [-7, 2, 9], [-3, 2, 5]] arrayOne [[-6, 3, 9], [-7, 2, 9], [-3, 2, 5]] arrayOne [[-6, 3, 9], [-7, 2, 9], [-3, 2, 5]] arrayOne [[-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]] arrayTwo等于[[3, 4, 1], [0, 5, 5], [2, 3, 1], [3, 4, 1]] arrayTwo [[3, 4, 1], [0, 5, 5], [2, 3, 1], [3, 4, 1]] arrayTwo [[3, 4, 1], [0, 5, 5], [2, 3, 1], [3, 4, 1]] arrayTwo [[3, 4, 1], [0, 5, 5], [2, 3, 1], [3, 4, 1]] arrayTwo [[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. Edit2:问题不在于方法copyArraycopyArray另一个方法。 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. 我编写的toString方法显示的是实例变量partitionResult所使用的值,而不是我传递给它的数组-这使得好像没有任何内容被复制。 The mistake should have been obvious to me. 这个错误对我来说应该是显而易见的。 I greatly appreciate the advice. 我非常感谢您的建议。

Though, one small bug was found by @Andrea. 但是,@ Andrea发现了一个小错误。

The error should be in 错误应该在

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

change it to 更改为

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

because your newly created array has to start inserting values from 0. 因为您新创建的数组必须从0开始插入值。

This should be simple enough, you're just making it hard for yourself by mutating end , making it hard to understand your loop's progression. 这应该足够简单,您只是通过更改end来使自己变得困难,从而使您难以理解循环的进程。 Just copy the values between begin and end (inclusive), but make sure to clone each subarray. 只需复制beginend (包括)之间的值,但请确保克隆每个子数组。 (The cloning effectively replaces your inner loop.) (克隆有效地替换了您的内部循环。)

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;
}

This gives the expected output for your sample input when calling foo(2) . 这将在调用foo(2)时为您的示例输入提供预期的输出。

if fromArray is the input array and index is the index at which you want to break input array, you could do: 如果fromArray是输入数组,而index是要中断输入数组的索引,则可以执行以下操作:

    System.arraycopy(fromArray, 0, arrayOne, 0, index);
    System.arraycopy(fromArray, index+1, arrayTwo, 0, fromArray.length-index);

What you have, when you create a 2d array, is an array of arrays. 创建二维数组时,您拥有的是数组数组。 Look at the problem like this: 看这样的问题:

[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、3、9],[-7、2、9],[-3、2、5],...,[3、4、1]]

We can represent your example data set as follows: 我们可以如下表示您的示例数据集:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM