• January 8th, 2014, 06:28 PM
gambron
Hello Coders i just registered and i love this site , i think i ll be here more often.

i have a question that i couldn't solve i'm new at java here is the question ;

Write a Java method called identical that takes 2 arrays of integers as arguments. The method should return true if the 2 arrays are identical; false otherwise. Hint: Two arrays are considered to be identical if they have the same length and the same values in the same order.
b. Include your method in part a) into a Java program that initializes two arrays, invokes (calls) the method identical to check if the two arrays are identical or not and print a suitable message.

And here is my code

Code :

```  public class identicalMethod {   public static void main(String[] args){     int[] array = {1,2,3,4,5}; int[] array2 = {1,2,3,4,10};   if (identical(array, array2)) System.out.println("Its identical"); else System.out.println("its not");     }       public static boolean identical(int[] a , int[] b) { boolean result = false ; for (int i = 0; i <a.length; i++){   if(a[i] == b[i]) result = true;         } return result;   } }```

It always telling me that it's identical but it's not
• January 8th, 2014, 11:54 PM
lym406365619
The identical() method is wrong!
1. You should determine the length of a and b is equal or not;
2. As you write, if a[i]==b[i], result is true.It means one value is same in a and b, the array of a and b is identical. You should determine all the numbers of a and b are equal or not.

Code :

` Code Removed`
• January 9th, 2014, 04:48 AM
GregBrannon
@lym406365619: Your description of a useful identical() method was sufficient. Let the OP figure out how to code it but continue to be helpful if needed and if you can. Be helpful, but don't do the work.
• January 9th, 2014, 07:15 AM
gambron
ohhh got it you mean to invert the if condition ,

Thank you so much appreciate it
• January 9th, 2014, 07:59 AM
Ganeprog
HI,

You just change the method like below, it will work fine.

public static boolean identical(int[] a , int[] b)
{
boolean result = true ;
for (int i = 0; i <a.length; i++){

if(a[i] != b[i])
result = false;

}
return result;

}

I changed only "if" condition then "flag" value tats it.
• January 9th, 2014, 08:03 AM
gambron