I have two separate 2D arrays. Each contains smaller arrays that are pairs of int values. The first 2D array is the input information, the other 2D array contains values that are to be matched or searched against.
For example, I the input array is:
{{0, 1}, {0, 2}, {2, 1}, {1, 0}, {0,1}}
The second, reference array is all possible combinations of values from the first:
{{0, 0}, {0,1}, {0,2}, {1, 0}, {1, 1}, {1, 2}, {2, 0}, {2, 1}, {2, 2}}
I want to take the input and determine if subarray i is equal to any of the reference subarrays. I want the results to be in a one-dimensional array the same size as the reference, with a count for the number of times each subarray was found in the input array. So the resulting array should look like this:
{0, 2, 1, 1, 0, 0, 0, 1, 0}
This is my attempt at the code:
int[] ResultArray = new int[ReferenceArray.Length];
for (int i = 0; i < ReferenceArray.Length; i++)
{
for (int j = 0; j < InputArray.length; j++)
{
if (InputArray[j] == RefereenceArray[i])
ResultArray[i]++;
}
}
For some reason, this compiles but nothing is added at all to ResultArray. When I tried to use
Arrays.equal(InputArray[j], ReferenceArray[i])
instead, I get an error in compiling. Any idea what has gone wrong? I had a similar block of code that worked fine, and can't figure out what the problem is here.
-
possible duplicate of Is it possible to check if two arrays are not equal in javaStackFlowed– StackFlowed2014年10月14日 15:27:08 +00:00Commented Oct 14, 2014 at 15:27
1 Answer 1
==
(on an Object
type) always tests reference identity. You can use Arrays.deepEquals(Object[], Object[])
by changing this
if (InputArray[j] == RefereenceArray[i])
to something like
if (Arrays.deepEquals(InputArray[j], RefereenceArray[i]))
Comments
Explore related questions
See similar questions with these tags.