比较j2me中的两个int数组?
在我的应用程序中,我有2个图像具有相同的尺寸,我会给他们的RGB数据并进行比较。在j2me中,我们不能使用java.util.Arrays等Arrays.equals(array1, array2)
方法。比较它们的一种方法是使用为循环并比较两个int数组中的每个元素,但是我正在寻找更好的方法。当我在web中搜索时,发现有一些equals()方法的ArrayUtils类here,但它是比较两个对象数组的方法,并且在比较int数组之前,它将它们转换为ArrayToEnumeration(Object数组)的枚举,该枚举根据给定对象创建枚举。
最后这是我的问题:
有没有更好的方法来比较j2me中的两个int数组?比较j2me中的两个int数组?
尝试这样的事情。从的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;
}
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();
:谢谢你的时间,我做到了,但正如我在问题中注意到的,我试图找到比“for”更好的方法。 – hasanghaforian 2012-03-01 11:43:49
而不是for,你可以使用迭代器来检查两个向量。 – 2012-03-01 11:49:23
转换字节数组字符串:
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;
}
}
:谢谢您的时间,我做到了,但正如我在这个问题通知我试图找到比“为”更好的办法。 – hasanghaforian 2012-03-01 11:43:22