问题
I am developing a program on Android that will compare the similarity of Gestures using Gesture Points. 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.
回答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:
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]);
}
}
回答3:
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:
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:
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.
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).
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:
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};
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]);
}
来源:https://stackoverflow.com/questions/15930540/compare-arrays-of-two-different-lengths