问题描述:

I am trying to find an efficient way to compare a given array that represents the Quality of Service of a Client with the QOS of a Two Web Services and pick up the most similar to the given QOS.

 int[] arrayCompare(int[] clientQOS){

int[] service1QOS=new int[]{3,1,4};

int[] service2QOS=new int[]{1,3,3};

//I want to compare clientQOS with S1 QOS and S2 QOS

return mostSimilarArray

}

网友答案:

Loop through the source array for each target, incrementing a counter by the delta (positive difference between the values). The smallest delta sum is the closest match.

Caveats

This should be true if you consider numerically closer to be more similar, and the scale between different values are equal.

网友答案:

I'd make a custom object instead of array and implement interface Comparable. This way you can encapsulate algorithm of comparison and reuse it later in other places.

相关阅读:
Top