+ 1

Count contents of array2 in array1

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}; array2 [][]={{10,20}, {10,20}} I want to count number of array2 content in array1

24th Mar 2017, 4:41 AM
Mayur Kale
Mayur Kale - avatar
10 Respuestas
+ 16
Here´s a code: https://code.sololearn.com/cGBdMNf5dTqJ I hope this isn't your homework. But your teacher will find out anyway, that you didn´t do it on your own if it is :D
24th Mar 2017, 9:01 PM
Tashi N
Tashi N - avatar
+ 15
What exactly do you want to do? Do you want to count, how often every single element of array2 is contained in array1?
24th Mar 2017, 2:31 PM
Tashi N
Tashi N - avatar
+ 14
i have no idea that you have no idea what you want to ask.
24th Mar 2017, 2:49 PM
Agus Mei
Agus Mei - avatar
+ 13
@Kenyatta 😂 I can imagine! I just hope he has to explain the code and why the utility classes I put there are used.
24th Mar 2017, 10:47 PM
Tashi N
Tashi N - avatar
+ 8
use double for each loops, or double iterators
24th Mar 2017, 5:33 AM
Meharban Singh
Meharban Singh - avatar
+ 6
FOR 1D ARRAY int count =0; for(int x = 0;x<array1.length;x++){ for(int y=0;y<array2.length;y++){ if(array1[x].equals(array2[y])){ count++; } }}
24th Mar 2017, 8:37 PM
Meharban Singh
Meharban Singh - avatar
+ 3
I want to display the number of count for array2 present in array1
24th Mar 2017, 6:57 PM
Mayur Kale
Mayur Kale - avatar
+ 1
but I dont have any idea about it please give me a code
24th Mar 2017, 6:37 AM
Mayur Kale
Mayur Kale - avatar
+ 1
This is static code which counts a number of times array2 present in array1 but I want a dynamic code for the same i.e. even if change int array2[][]={{10,20,30},{10,20,40}} it should show 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++); } please give me a dynamic code for same
28th Mar 2017, 7:32 AM
Mayur Kale
Mayur Kale - avatar