java 比较两个不同长度的数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15930540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:18:49  来源:igfitidea点击:

compare arrays of two different lengths

javaandroidarrayssimilarity

提问by rachel

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.

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

采纳答案by phcoding

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

回答by Zim-Zam O'Pootertoot

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

回答by duggu

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

回答by Majid Abarghooei

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

回答by Franklin

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(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.

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

回答by Kona Suresh

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

    }