日期:2014-05-17 浏览次数:20558 次
int im = 20;
int[] MyList = new int[] {6,15,23,59,80,87,120};
Array.Sort(MyList);
for(int i =0;i <MyList.Length -1 ;i++)
{
if(MyList[i]<82 && MyList[i+1]>82){
MessageBox.Show(i.ToString());
}
}
------解决方案--------------------
int im = 20;
int[] MyList = new int[] {6,15,23,59,80,87,120};
Array.Sort(MyList);
int index = Array.BinarySearch(MyList, im);
if (index < 0)
{
index = ~index;
}
Console.WriteLine(
"Value >= {0} is {1}, value < {0} is {2}.", im,
index == MyList.Length ? "not exists" : MyList[index].ToString(),
index == 0 ? "not exists" : MyList[index - 1].ToString()
);