我正在 Android 上开发一个程序,该程序将使用手势点来比较手势的相似性。我有两个这样的数组:
gest_1 = [120,333,453,564,234,531]
gest_2 = [222,432,11,234,223,344,534,523,432,234]
我知道无法动态调整任一数组的大小,所以有什么方法可以让我使用这些数组来比较这两个手势并返回相似度吗?
请注意,数组中的数据只是随机输入的。
最佳答案
使用哈希集。对于两个列表的并集,
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]);
对于两个列表的交集,
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]);
}
}
关于java - 比较两个不同长度的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15930540/