【问题标题】:compare two int array in j2me?比较j2me中的两个int数组?
【发布时间】:2012-03-19 17:28:47
【问题描述】:

在我的应用程序中,我有 2 张具有相同尺寸的图像,我将提供它们的 RGB 数据并进行比较。在 j2me 中,我们不能使用 java.util.ArraysArrays.equals(array1, array2) 方法.比较它们的一种方法是使用 for 循环并比较两个 int 数组的每个元素,但我正在寻找更好的方法。当我在网上搜索时,我发现 ArrayUtils 类here,它有一些 equals() 方法,但它是比较两个对象数组的方法,在比较 int 数组之前将它们转换为 Enumeration by arrayToEnumeration(Object array),从给定对象创建一个枚举。
最后这是我的问题:
有没有更好的方法来比较 j2me 中的两个 int 数组?

【问题讨论】:

    标签: arrays java-me compare


    【解决方案1】:

    试试这样的..来自 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;
    }
    

    【讨论】:

    • :感谢您的宝贵时间,我做到了,但正如我在问题中注意到的那样,我试图找到比“for”更好的方法。
    【解决方案2】:
    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”更好的方法。
    • 您可以使用迭代器代替 for 来检查两个向量。
    【解决方案3】:

    将字节数组转换为字符串:

     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;
         }
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-26
      相关资源
      最近更新 更多