2012-03-01 142 views
1

在我的應用程序中,我有2個圖像具有相同的尺寸,我會給他們的RGB數據並進行比較。在j2me中,我們不能使用java.util.ArraysArrays.equals(array1, array2)方法。比較它們的一種方法是使用循環並比較兩個int數組中的每個元素,但是我正在尋找更好的方法。當我在web中搜索時,發現有一些equals()方法的ArrayUtils類here,但它是比較兩個對象數組的方法,並且在比較int數組之前,它將它們轉換爲ArrayToEnumeration(Object數組)的枚舉,該枚舉根據給定對象創建枚舉。
最後這是我的問題:
有沒有更好的方法來比較j2me中的兩個int數組?比較j2me中的兩個int數組?

回答

0

嘗試這樣的事情。從的Util類

public static boolean equals(byte[] a, byte[] a2) { 
    if (a==a2) 
     return true; 
    if (a==null || a2==null) 
     return false; 

    int length = a.length; 
    if (a2.length != length) 
     return false; 

    for (int i=0; i<length; i++) 
     if (a[i] != a2[i]) 
      return false; 

    return true; 
} 
+0

:謝謝您的時間,我做到了,但正如我在這個問題通知我試圖找到比「爲」更好的辦法。 – hasanghaforian 2012-03-01 11:43:22

0
private int compareArray(String[] _array1, String[] _array2){ 

Vector m_length1 = new Vector(); 
Vector m_length2 = new Vector(); 

if(_array1 && _array2!=null) 
{ 
for(int i=0; i<_array1.length; i++){ 
if(m_length1[i]!=null){ 
m_length1.add(_array1[i]); 
} 
} 
for(int j=0; j<_array2.length; j++){ 
if(m_length2[j]!=null){ 
m_length2.add(_array2[j]); 
} 
} 
} 
if(m_length1.size()==m_length2.size()){ 
return 0; /*matching*/ 
}else{ 
return -1; /*no matching*/ 
} 
} 

你可以修改它作爲int數組或者你可以比較每個值的字節。例如, ;

byte sample[] = {0,0,0,0}; 
sample= yourValue.getBytes(); 
+0

:謝謝你的時間,我做到了,但正如我在問題中注意到的,我試圖找到比「for」更好的方法。 – hasanghaforian 2012-03-01 11:43:49

+0

而不是for,你可以使用迭代器來檢查兩個向量。 – 2012-03-01 11:49:23

0

轉換字節數組字符串:

public boolean equals(byte[] b1, byte[] b2){ 
    String strB1 = new String(b1); 
    String strB2 = new String(b2); 
    if(strB1.equals(strB2)){ 
     return true; 
    } 
    else{ 
     return false; 
    } 
}