Tuesday, 29 March 2016

Check if all the references in array1 are equal to all the references in array2


public static boolean equalReferencesInArrays(final Object[] array1,
    final Object[] array2) {
      if (array1 == null) {
          return (array2 == null);
      }      
      if (array2 == null) {
          return false;
      }
      if (array1.length != array2.length) {
          return false;
      }

      for (int i = 0; i < array1.length; i++) {
          if (array1[i] == null) {
              if (array2[i] != null) {
                  return false;
              }
          }
          if (array2[i] == null) {
              if (array1[i] != null) {
                  return false;
              }
          }
          if (array1[i] != array2[i]) {
              return false;
          }
      }
      return true;
  }

No comments:

Post a Comment

Note: only a member of this blog may post a comment.