Powered by Ajaxy
Digg StumbleUpon LinkedIn YouTube Flickr Facebook Twitter RSS Reset

Java: How to compare two-dimensional arrays

Java: How to compare two-dimensional arrays
{filelink=4668}

import java.util.Arrays;
public class ComparaisonTableau {
/*
 * This function returns true
 * if two array are identical
 */
  public static boolean arrayIsEquals(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 (!Arrays.equals(array1[i], array2[i])) {
              return false;
          }
      }
      return true;
  }
    public static void main (String[] argv)
  	{
  	  Integer tab1[][]=	{{1, 2, 3},{4, 5, 6}};
  	  Integer tab2[][]=	{{-1, -2, 3},{ -4, -5,-6}};
  	  Integer tab3[][]=	{{1, 2, 3},{4, 5, 6}};
  	  // Tester les tableaux
  	  System.out.println ("Tab1 == Tab2? "+arrayIsEquals(tab1,tab2));   //false
  	  System.out.println ("Tab1 == Tab3? "+arrayIsEquals(tab1,tab3));   //true
    }
}

Users who found this page were searching for:

  • java JButton examples JDK7
  • comparing identical arrays in java
  • iterate over tuple

No comments yet.

Leave a Comment