/**
* @Description: 统计以split分割scores数量
* @param scores
* @param split:分割段,>0,[60,10,20,10]-->分割为[0-59,60-69,70-89,90-100],统计段内数量
* @return: void
*/
public void analyseData(double[] scores,int[] split){
Arrays.sort(scores);
Map numMap=new LinkedHashMap();
int[] splitScore=new int[split.length];
splitScore[0]=split[0];
int lastValue = 0,index=0;
index = Arrays.binarySearch(scores, splitScore[0]);
if (index > 0) {
//是否有重复值
for(int i=index-1;i>=0;i--){
if(scores[i]==splitScore[0]){
index--;
}else{
break;
}
}
if(index>0){
numMap.put(String.format("<%s",splitScore[0]),index - lastValue);
lastValue=index;
}
}
for(int i=1,len=split.length;i=0;j--){
if(scores[j]==splitScore[i]){
index--;
}else{
break;
}
}
}
//存在大于指定值情况时,最大值累计在最后一段
if(i==len-1){
index=Math.max(scores.length, index);
numMap.put(String.format("[%s-%s]", splitScore[i-1],splitScore[i]),index-lastValue);
}else{
numMap.put(String.format("[%s-%s)", splitScore[i-1],splitScore[i]),index-lastValue);
lastValue=index;
}
}
for (Entry entry: numMap.entrySet()) {
System.out.println(entry.getKey()+"--="+entry.getValue());
}
}
测试:
double []array4={-0.1,-0.1,0,0,0,0,0.1,0.1,0.1,0.5,5,5,5,10,11,12,15,15,15,34,34,35,35,54,54,55,55,64,64,65,65,64,65,66,66,100,101,101,110};
int[] split={0,1,5,10,20,20,10,35};
t.analyseData(array4, split);
结果: