+ 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
10 Antworten
+ 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
+ 15
What exactly do you want to do? Do you want to count, how often every single element of array2 is contained in array1?
+ 14
i have no idea that you have no idea what you want to ask.
+ 13
@Kenyatta
😂 I can imagine! I just hope he has to explain the code and why the utility classes I put there are used.
+ 8
use double for each loops, or double iterators
+ 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++;
}
}}
+ 3
I want to display the number of count for array2 present in array1
+ 1
but I dont have any idea about it please give me a code
+ 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