比较两个不同长度的数组

时间:2021-10-12 12:02:41

I am developing a program on Android that will compare the similarity of Gestures using Gesture Points. I have two arrays like this:

我正在Android上开发一个程序,用手势点来比较手势的相似度。我有两个这样的数组:

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.

注意数组中的数据是随机输入的。

6 个解决方案

#1


1  

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

#2


5  

Use a HashSet. For the union of the two lists,

使用一个HashSet。对于这两个列表的联合,

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

#3


1  

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

#4


0  

        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 ...");
    }

#5


0  

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.

空间有关系吗?如果不是,可以将其中一个数组存储在hashtable中,然后遍历另一个数组,检查该元素是否包含在hashtable中。这是O(n)而不是O(nm)但这也会增加算法的大小。

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.

上面的想法假设当你说“相似点”时,意味着一个数组中的任何元素都等于另一个数组中的任何其他元素。

#6


-2  

We consider the two arrays like this
int[] array1={3,5,4,2,6,1,7,9,8}; int[] array2={1,2,3,4,8};

我们考虑这样的两个数组:int[] array1={3,5,4,2,6,1,7,9,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]);

    }

#1


1  

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

#2


5  

Use a HashSet. For the union of the two lists,

使用一个HashSet。对于这两个列表的联合,

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

#3


1  

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

#4


0  

        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 ...");
    }

#5


0  

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.

空间有关系吗?如果不是,可以将其中一个数组存储在hashtable中,然后遍历另一个数组,检查该元素是否包含在hashtable中。这是O(n)而不是O(nm)但这也会增加算法的大小。

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.

上面的想法假设当你说“相似点”时,意味着一个数组中的任何元素都等于另一个数组中的任何其他元素。

#6


-2  

We consider the two arrays like this
int[] array1={3,5,4,2,6,1,7,9,8}; int[] array2={1,2,3,4,8};

我们考虑这样的两个数组:int[] array1={3,5,4,2,6,1,7,9,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]);

    }