Reputation: 177
Basically, I am trying to compare 2D arrays with an array, and if an element matches in single array with 2D array, the 2D array replaces the value of single array. I'll give an example:
So, there are two 2D arrays:
arrayOne = [[livesAt, fname, city], [worksAt, fname, company]]
arrayTwo = [[livesAt, x, y], [worksAt, x, z]]
And a single array:
findOut = [x,y,z]
The aim is arrayOne replaces everything in arrayTwo, and find out the elements in findOut array by comparing with arrayTwo.
Edit: The value of "x" reads the values of the location in the arrays... which is fname. So, the value of "x" has to be the same from the first row and second row (fname).
The answer should be:
arrayOne = [[livesAt, fname, city], [worksAt, fname, company]]
arrayTwo = [[livesAt, fname, city], [worksAt, fname, company]]
findOut = [fname,city,company]
I have attempted this in my code, like this:
for (int i = 0; i < arrayOne.length;i++ ) {
for (int j = 0; j < arrayTwo.length; j++){
for(int x = 0; x < findOut.length;x++){
if(arrayTwo[i][j].equals(findOut[x])) {
arrayTwo[i] = arrayOne[i];
arrayTwo[j] = arrayOne[j];
findOut[x] = arrayOne[i][j];
System.out.println("Match found");
}else{
System.out.println("Not found");
}
}
}
}
Most of it works except the findOut array only replaces the first element 'x' and not the others.
[fname, y, z]
And during the loop, it prints:
Not found
Not found
Not found
Match found
Not found
Not found
Not found
Not found
Not found
Match found
Not found
Not found
I strongly feel there is a problem with my loop. Can anyone give me any pointers or suggestions on how to solve this? I have been trying to figure this out, but only made small progress.
Upvotes: 1
Views: 3732
Reputation: 5215
Iterating through arrayOne
is not required as we are comparing arrayTwo
and findOut
Please note that as @SpiderPig pointed out this has fname
gname
problem (findOut
values will be over written if x
refers to two values in arrayOne
) though arrayTwo
values will be updated properly
public static void method(String[][] arrayOne, String[][] arrayTwo, String[] findOut)
{
String[] findOutClone = findOut.clone();//Required for comparison, as we are mutating findOut
for (int i = 0; i < arrayTwo.length; i++)
{
String[] arrTwo = arrayTwo[i];
for (int j = 0; j < arrTwo.length; j++)
{
String arrTwoEl = arrTwo[j];
for (int k = 0; k < findOutClone.length; k++)
if (arrTwoEl.equals(findOutClone[k]))
arrTwo[j] = findOut[k] = arrayOne[i][j];
}
}
}
Upvotes: 0
Reputation: 3641
if(arrayTwo[i][j].equals(findOut[x])) {
arrayTwo[i] = arrayOne[i]; // Here you are replacing the entire element. What you need to do is only replace that particular value using [i][j]
arrayTwo[j] = arrayOne[j];
findOut[x] = arrayOne[i][j];
}
What you need to do is,
if(arrayTwo[i][j].equals(findOut[x])) {
arrayTwo[i][j] = arrayOne[i][j];
findOut[x] = arrayOne[i][j];
}
Also, .length of a 2-dimensional array would give you the number of arrays in it. Not the number of elements in each of the array in it. So in order to fetch the proper length, you have to fetch an array within it and find its length.
for (int i = 0; i < arrayOne.length; i++) {
for (int j = 0; j < arrayTwo[i].length; j++) {
for (int x = 0; x < findOut.length; x++) {
if (arrayTwo[i][j].equals(findOut[x])) {
findOut[x] = arrayOne[i][j];
System.out.println("Match found");
} else {
System.out.println("Not found");
}
}
}
}
Upvotes: 0
Reputation: 8139
Your question is not clear
What happens if the arrays look like this?
arrayOne = [[livesAt, fname, city], [worksAt, gname, company]]
arrayTwo = [[livesAt, x, y], [worksAt, x, z]]
findOut = [x,y,z]
will the result be this
arrayOne = [[livesAt, fname, city], [worksAt, gname, company]]
arrayTwo = [[livesAt, fname, city], [worksAt, gname, company]]
findOut = [fname,city,company]
or this
arrayOne = [[livesAt, fname, city], [worksAt, gname, company]]
arrayTwo = [[livesAt, fname, city], [worksAt, gname, company]]
findOut = [gname,city,company]
edit: Try this
import java.util.Arrays;
public class Test {
private static int indexOf(String[] array, String str) {
for(int i = 0; i < array.length; i++) if(array[i].equals(str)) return i;
return -1;
}
private static String toString(String[][] array) {
String str = "";
for(String[] sa: array) str += ", " + Arrays.toString(sa);
return "[" + str.substring(2) + "]";
}
public static void main(String[] args) {
String[][] arrayOne = {{"livesAt", "fname", "city"}, {"worksAt", "fname", "company"}};
String[][] arrayTwo = {{"livesAt", "x", "y"}, {"worksAt", "x", "z"}};
String[] findOut = {"x","y","z"};
String[] newFindOut = findOut.clone();
for(int y = 0; y < arrayTwo.length; y++) {
for(int x = 0; x < arrayTwo[y].length; x++) {
int i = indexOf(findOut, arrayTwo[y][x]);
if(i >= 0) {
arrayTwo[y][x] = arrayOne[y][x];
newFindOut[i] = arrayOne[y][x];
}
}
}
findOut = newFindOut;
System.out.println("arrayOne = " + toString(arrayOne));
System.out.println("arrayTwo = " + toString(arrayTwo));
System.out.println("findOut = " + Arrays.toString(findOut));
}
}
Upvotes: 0
Reputation: 929
The problem is; when you give your for loop condition with i < arrayTwo.length
, it just iterates for only 2 times because arrayTwo
has only 2 arrays in it. But you want to traverse those 2 array's all elements and you are not providing a loop for it.
Also, when you find a match, you both change arrayTwo
and findOut
array values dynamically and that will cause a mismatch when you try to find same elements' match. So it works wrong. You need to store previous values of findOut
in order to compare those values(x, y, z
) again in case of duplicate values(i.e you have two x
values in arrayTwo
and at first match, you change x
with fname
, so there won't be any second match). You can do it like this:
String[] temp = findOut.clone();
for (int i = 0; i < arrayTwo.length; i++) {
for (int j = 0; j < arrayTwo[i].length; j++) {
for (int x = 0; x < temp.length; x++) {
if (arrayTwo[i][j].equals(temp[x])) {
arrayTwo[i][j] = arrayOne[i][j];
findOut[x] = arrayTwo[i][j];
System.out.println("Match found");
}
else {
System.out.println("Not found");
}
}
}
}
And the arrays will change as you expected:
arrayOne = [[livesAt, fname, city], [worksAt, fname, company]]
arrayTwo = [[livesAt, fname, city], [worksAt, fname, company]]
findOut = [fname,city,company]
Upvotes: 1
Reputation: 2154
I don't think these 2 lines are correct:
arrayTwo[i] = arrayOne[i];
arrayTwo[j] = arrayOne[j];
Because arrayTwo[i] = arrayOne[i]
will cause the if statement to always be true:
if(arrayTwo[i][j].equals(findOut[x]))
Shouldn't it be arrayTwo[i][j] = arrayOne[i][j];
Upvotes: 0