简体   繁体   English

比较两个不同长度的数组

[英]compare arrays of two different lengths

I am developing a program on Android that will compare the similarity of Gestures using Gesture Points. 我正在Android上开发一个程序,该程序将使用“手势点”比较手势的相似性。 I have two arrays like this: 我有两个像这样的数组:

gest_1 = [120,333,453,564,234,531]
gest_2 = [222,432,11,234,223,344,534,523,432,234]

I know there is no way to dynamically resize either one of the arrays, so is there any way for me to compare both these gestures using these arrays and return the similarity? 我知道没有办法动态调整其中一个数组的大小,所以我有什么办法可以使用这些数组比较这两个手势并返回相似性吗?

Note that the data in the arrays are just randomly typed out. 注意,数组中的数据只是随机输入的。

Use a HashSet. 使用HashSet。 For the union of the two lists, 对于两个列表的并集,

HashSet<Integer> hashSet = new HashSet<>(); // Contains the union
for(int i = 0; i < array1.length; i++)
    hashSet.add(array1[i]);
for(int i = 0; i < array2.length; i++)
    hashSet.add(array2[i]);

For the intersection of the two lists, 对于两个列表的交集,

HashSet<Integer> hashSet = new HashSet<>();
List<Integer> list = new ArrayList<>();  // Contains the intersection
for(int i = 0; i < array1.length; i++)
    hashSet.add(array1[i]);
for(int i = 0; i < array2.length; i++) {
    if(hashSet.contains(array2[i])) {
        list.add(array2[i]);
    }
}

You could try something like this: 您可以尝试这样的事情:

  List similarities = new ArrayList();
  for(int i = 0; i < Math.max(gest_1.length, gest_2.length); i++){
    if (gest_1[i] == gest_2[i])
       similarities.add(gest_1[i];
  }

Try this function it return array:- 试试这个函数返回数组:

public static String[] numSame (String[] list1, String[] list2) 
     {  
          int same = 0;  
          for (int i = 0; i <= list1.length-1; i++) 
          {  
             for(int j = 0; j <= list2.length-1; j++) 
             {  
                if (list1[i].equals(list2[j])) 
                {  
                    same++;  
                    break;  
                }  
             }  
          }  

          String [] array=new String[same];
          int p=0;
          for (int i = 0; i <= list1.length-1; i++) 
          {  
             for(int j = 0; j <= list2.length-1; j++) 
             {  
                if (list1[i].equals(list2[j])) 
                {  
                    array[p]=  list1[i]+"";
                    System.out.println("array[p] => "+array[p]);
                    p++;
                    break;  
                }  
             }  
          } 
          return array;
       }  
        int temp = 0;
        int[] gest_1 = {120, 333, 453, 564, 234, 531};
        int[] gest_2 = {222, 432, 11, 234, 223, 344, 534, 523, 432, 234};
        ArrayList<Integer> g1 = new ArrayList<>();
        ArrayList<Integer> g2 = new ArrayList<>();

        for (int i : gest_1) {
            g1.add(i);
        }
        for (int i : gest_2) {
            g2.add(i);
        }
        for (int i : gest_1) {
            if (g2.contains(i)) {
                temp++;
            }
//            else{
//                break;
//            }
        }

        System.out.println(temp + " element(s) are equal ...");
    }

Does space matter? 空间重要吗? If not, you could store one of the arrays in a hashtable, and then iterate through the other array checking if the element is contained in the hashtable. 如果没有,您可以将一个数组存储在哈希表中,然后遍历另一个数组,检查该元素是否包含在哈希表中。 This would be O(n) as opposed to O(nm), but this would also add to the size of the algorithm. 与O(nm)相比,这将是O(n),但这也会增加算法的大小。

If you are unable to do such a thing, it would require two loops. 如果您无法执行此操作,则需要两个循环。 The outer loop would increment the index of the first array after the inner loop has incremented through the entire second array checking if the elements are equal along the way. 在内部循环遍历整个第二个数组之后,如果检查沿途元素是否相等,则外部循环将增加第一个数组的索引。 This could potentially be O(nm). 这可能是O(nm)。

The above thoughts are assuming that when you say "similarities" it means that there are any elements in one array equal to any of the other elements in the other array. 上面的想法是假设当您说“相似性”时,它意味着一个数组中的任何元素等于另一个数组中的任何其他元素。

We consider the two arrays like this 我们考虑这样的两个数组
int[] array1={3,5,4,2,6,1,7,9,8}; int [] array1 = {3,5,4,2,6,1,7,9,8}; int[] array2={1,2,3,4,8}; int [] array2 = {1,2,3,4,8};

Our aim is find the similar values. 我们的目标是找到相似的值。

    int[] res;

    if(array1.length>array2.length){
         res=new int[array2.length];
    }else{
        res=new int[array1.length];
    }

      int k=0;

    for(int i=0;i<array1.length;i++)
            {
            for(int j=0;j<array2.length;j++)
                {
                    if(array1[i]==(array2[j]))
                        {
                        res[k]=array1[i];
                            k++;
                            break;

                       }

                }
            }

    for(int l=0;l<res.length;l++){


        System.out.print(res[l]);

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM