-3

Whenever I try to run this method to compare arrays I always get an ArrayIndexOutOfBoundsException. What am I doing wrong?

public static boolean areIdentical(int[] m, int[] n) {
   //write a loop that compares each individual element of the two arrays.
   //if a mismatch is found, return false
   boolean identical = false;
   for(int x = 0; m.length > x || n.length > x; x++) {
     if(m[x] == n[x]) {
       identical = true; 
     } else {
       identical = false;  
     }
   }
   return identical;
}
Vitruvie
  • 2,327
  • 18
  • 25
Jack
  • 11
  • 3
  • Not the cause of the error, but do note that your statement setting `identical = true` causes this to give incorrect output, because it overwrites if you had set `identical = false` earlier. – Vitruvie Mar 06 '15 at 04:49

3 Answers3

2

The problem is, you're using the disjunction OR. This means that as long as your index is in range for at least one of the arrays, it will attempt to access it.

You should change your condition to:

m.length > x && n.length > x

Or, better yet, check to see if they are the same length beforehand. If they aren't, then they can't be identical. However, I don't know the full program requirements here, so this might not be the intended behavior.

if(m.length != n.length){
    return false;
}
boolean identical = false; 
//...
Obicere
  • 2,999
  • 3
  • 20
  • 31
1

First try comparing the .length of the two arrays to see if they are equal. If not then they are not identical.

if they are the same length try changing your for loop to something like this

boolean identical = true;
if(m.length() != n.length()) identical = false;
if(identical)
{
 for(int x = 0; x < m.length(); x++)
 {
   if(m[x] != n[x])
   { 
     identical = false;
   }
 }
}
return identical;

this code should work as it seems you are just looking for any mismatch not all

Blake
  • 151
  • 2
  • 11
0

You can do one thing first calculate the length of small array.

public static boolean areIdentical(int[] m, int[] n)
{

    int length;
    if(m.length<n.length){
        length=m.length
    }else{
        length=n.length;
    }
   boolean identical = false;
    for(int x = 0; x<length; x++){

     if(m[x] == n[x]){
       identical = true; 
        break;
     }
     else
     {
      identical = false;  
      }
   }

    //write a loop that compares each individual element of the two arrays.
    //if a mismatch is found, return false

    return identical;
}

Benefit of earlier calculate length is you dont need to do && or || for each iteration.And use break when you find them identical it will save your iteration.

But as your comment said you need to check individual element of both array i guess you need to use 2 for loops:-

boolean identical = false;
    for(int x = 0; x<n.length; x++){
        for(int y=0;y<m.length;y++){

             if(m[y] == n[x]){
               identical = true; 
               break;
             }
           }
           }
return identical;
squiroid
  • 13,809
  • 6
  • 47
  • 67