Compare 2 two-dimensional arrays and append equal rows to a new empty [][]array
in
Programming Questions
•
1 year ago
Hi everyone,
I am now trying to filter [][] array1 through [][]array2 (array1 and array2 have different number of rows and same number of columns). If rows of two arrays match, they are appended to an empty [][] array3.
I need the length of the resulting [][]array3 be flexible, not pre-defined in advance. However, if I initialise it as int [][]array3 = new int [0][0] I get ArrayIndexOutofBoundsException error...
Alternatively, if I set the length as maximum possible length of array3, I get empty rows filled with zeros which I do not need. Does anyone know how to fix this problem?
Thank you very much:)
Here is the code:
int [][] array1 = { //all possible combinations
{1, 2, 3, 4},
{0, 1, 2, 4},
{3, 4, 5, 6},
{2, 3, 9, 3},
{3, 4, 6, 8},
{3, 4, 12, 13}};
int [][] array2 = {//all combinations for this player
{1, 2, 3, 4},
{0, 1, 2, 4},
{3, 4, 5, 6},
{2, 3, 10, 11},
{3, 4, 12, 13}};
int [][]array3 = new int [6][4];
void setup() {
int count=-1;
for (int i=0; i<array1.length;i++) {
for (int j=0;j<array2.length;j++) {
if (Arrays.equals(array1[i], array2[j])) {
count++;
array3[count]=array2[j];
}
}
}
1