1

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.

msrd0
8,51010 gold badges62 silver badges94 bronze badges
asked Oct 14, 2014 at 15:21
1

1 Answer 1

4

== (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]))
answered Oct 14, 2014 at 15:26

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.