0

Want a dynamic java code to count number of time array2 present in array1

I have this static code to count, how many times array2 is present in array1, but I need a dynamic code i.e. even if I change array2 to {{10,20,30},{10,20,30}} it should give a proper count. This is a static code: public static void main(String[] args) { int array1[][]= {{10,20,30,40,50,60},{10,20,40,50,70,60},{10,20,30,40,50,60},{60,70,50,40,10,20},{80,90,70,60,10,20},{60,70,50,40,10,20}}; int array2[][]= {{10,20},{10,20}}; for (int i = 0; i < array1.length; i++) { for (int j = 0; j < array1[0].length; j++) { System.out.print(array1[i][j]+"\t"); } System.out.println(); } int cnt=0; for (int i = 0; i < array1.length-1; i++) { for (int j = 0; j < array1[0].length-1; j++) { for (int k = 0; k < array2.length-1; k++) { for (int l = 0; l < array2.length-1; l++) { if((array1[i][j]==array2[k][l])&&(array1[i][j+1]==array2[k][l+1])&&(array1[i+1][j]==array2[k+1][l])&&(array1[i+1][j+1]==array2[k+1][l+1])) { cnt++; } } } } } System.out.println("\nCount for array2 present in array1 is: "+cnt++); }

28th Mar 2017, 6:46 AM
Purshottam Kale
Purshottam Kale - avatar
3 Respuestas
28th Mar 2017, 2:37 PM
Tashi N
Tashi N - avatar
0
No!!! I want a count of whole 2D array in another 2D array.
29th Mar 2017, 4:58 PM
Purshottam Kale
Purshottam Kale - avatar
0
Please run the code which I have given above. You will understand it properly. The code which I have given above is static but I want it dynamic i.e. even if I change array2[][]={{20,30},{20,40}} the output must be 1 as it is present for 1 time in array1.
29th Mar 2017, 5:02 PM
Purshottam Kale
Purshottam Kale - avatar