2012-02-29 21 views
0

これを使って2つの2次元配列を比較しようとしています。しかし、 "の配列は同じではありません。"という文字列が同じになっても、私は続けています。2次元配列を比較する

 int i; 
    int j = 0; 
    int k; 
    int l; 

List<Integer> list = new ArrayList<Integer>(); 
List<Integer> list1 = new ArrayList<Integer>(); 
List<Integer> zero = new ArrayList<Integer>(); 

for (i = 1; i <= 16; i++) { 
    list.add(i); 


} 

//System.out.println(list); //[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] 
Collections.shuffle(list.subList(1, 15)); 
System.out.println(list); 

Collections.replaceAll(list, 16, 0); 
    System.out.println(list); 


// System.out.println(list); //[11, 5, 10, 9, 7, 0, 6, 1, 3, 14, 2, 4, 15, 13, 12, 8] 

int[][] a2 = new int[4][4]; 
int [][] a3 = new int[4][4]; 
for (i = 0; i < 4; i++) { 
    for (j = 0; j< 4; j++) { 


     a2[i][j] = list.get(i*4 + j); 
     a3[i][j] = list.get(i*4 + j); 
    } 

} 
for (int[] row : a2) { 
System.out.print("["); 
for ( int a : row) 
    System.out.printf("%4d", a); 
System.out.println("]"); 
} 
for (int[] row : a3) { 
System.out.print("["); 
for ( int a : row) 
    System.out.printf("%4d", a); 
System.out.println("]"); 
} 
boolean check1 = Arrays.equals(a2, a3); 
if(check1 == false) 
System.out.println("Arrays are not same."); 
else 
System.out.println("Both Arrays are same."); 

このようにすることはできません。 ブールcheck1 = Arrays.equals(a2 [i] [j]、a3 [i] [j]);

+2

これは、そのような2D配列を比較できないためです。 –

+0

どうすればいいですか? –

+0

一度に1つの1D行をチェックすることによって。 –

答えて

4

2次元配列は実際には配列の配列(つまりオブジェクトの配列)であるため、最初の配列は機能しません。オブジェクト配列のArrays.equals()メソッドは、equals()を使用して、対応する要素が等しいかどうかをテストします。残念ながらコードの場合、配列のequals()はデフォルトのObject実装です。同一オブジェクトの場合にのみequal()になります。あなたの場合、彼らはそうではありません。 をコードし、intの値を2つ渡すと、コンパイラはArraysクラスのシグネチャと一致させることができません。平等をチェックする

一つの方法は、deepEqualsを使用することです:

boolean check1 = Arrays.deepEquals(a2, a3); 

もう一つの方法は、明示的に外側の配列を反復処理することです:

boolean check1 = true; 
for (int i = 0; check1 && i < a2.length; ++i) { 
    check1 = Arrays.equals(a2[i], a3[i]); 
} 
3
boolean check1 = Arrays.deepEquals(a2, a3); 

は間違いなく可能性があります。その実装では、Object[]が使用されます。これは、オンザフライで渡す配列のタイプをチェックするため、魅力的かもしれません。

しかし、より厳密な入力と少ないオーバーヘッドが必要な場合は、次のように独自に記述することができます。 2Dアレイ上

import java.util.Arrays; 

/** 
* Operations on multi-dimensional arrays. 
* 
* @author stephen harrison 
*/ 
public class ArrayUtils { 
    private ArrayUtils() { 
     // Static methods only 
    } 

    public static <T> boolean equals(final T[][] a, final T[][] b) { 
     if (a == b) { 
      return true; 
     } 

     if (a == null || b == null) { 
      return false; 
     } 

     if (a.length != b.length) { 
      return false; 
     } 

     for (int i = 0; i < a.length; ++i) { 
      if (!Arrays.equals(a[i], b[i])) { 
       return false; 
      } 
     } 

     return true; 
    } 

    public static <T> boolean equals(final T[][][] a, final T[][][] b) { 
     if (a == b) { 
      return true; 
     } 

     if (a == null || b == null) { 
      return false; 
     } 

     if (a.length != b.length) { 
      return false; 
     } 

     for (int i = 0; i < a.length; ++i) { 
      if (!equals(a[i], b[i])) { 
       return false; 
      } 
     } 

     return true; 
    } 
} 

最初equalsArrays.equals()を呼び出します。 3Dバージョンも同様に2Dバージョンを呼び出します。

私は役立つことを願っています。