I need help with some java code concerning the comparison of two arrays. Let\'s
ID: 3602402 • Letter: I
Question
I need help with some java code concerning the comparison of two arrays.
Let's say I have two arrays. Array A and Array B. Array A has 7 integers on each line, and there are hundreds of lines. Array B is the same as Array A except every line of integers have been sorted in ascending order. In Array A there are already lines that are sorted, so some lines in Array A will be equal in Array B. What code could I write to I find out which lines in the arrays are equal and print only those lines out using the Arrays.equals method in java?
Explanation / Answer
public class Array_equals
{
//Takes Array A and B and their size as rowA,rowB and column size as size
public static void Compare(int[][] A,int [][] B,int rowA,int rowB,int size)
{
//for each row in array A
for(int i=0;i<rowA;i++)
{
//for each row in array B
for(int j=0;j<rowB;j++)
{
//check is a flag, it is true
//only when all the elements are equal
boolean check=true;
//checking all the elements are equal or not
for(int k=0;k<size;k++)
{
if(A[i][k]!=B[j][k])
{
check=false;
break;
}
}
//if equal,print row number of A
if(check)
System.out.println(i);
}
}
}
//main method
public static void main(String args[])
{
//for simplisity, i passed only less size of arrays
int[][] arr1={{1,2,3},{2,3,4}};
int[][] arr2={{2,3,4},{3,4,5}};
Compare(arr1,arr2,2,2,3);
}
}