[[-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.