比较两种不同长度的数组

rac*_*ach 2 java arrays android similarity

我正在开发一个Android程序,它将使用手势点比较手势的相似性.我有两个像这样的数组:

gest_1 = [120,333,453,564,234,531]
gest_2 = [222,432,11,234,223,344,534,523,432,234]
Run Code Online (Sandbox Code Playgroud)

我知道没有办法动态调整其中一个数组的大小,所以有没有办法让我用这些数组比较这些手势并返回相似性?

请注意,数组中的数据只是随机输出.

Zim*_*oot 5

使用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]);
Run Code Online (Sandbox Code Playgroud)

对于两个列表的交集,

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]);
    }
}
Run Code Online (Sandbox Code Playgroud)