如何做线性我想获得数组中要插入的项目的位置,我的意思是我必须在数组中插入数据的索引。我怎样才能通过仅使用线性搜索来实现这一点。请提出建议。
问问题
611 次
2 回答
0
找到最接近的可能值并根据您的需要在它之前或之后插入
loop
{
int index = [arrResultRow indexOfObject:10];
[arrResultRow insertObject:object atIndex:index+1]
}
于 2012-04-02T13:17:46.600 回答
0
例如使用 for 循环进行线性搜索:
int[] test = new int[1024];
// i assume you have something like this
int searchnumber = 17;
int foundindex = -1;
for(int i = 0; i < count, i++)
{
if (test[i] == 17)
{
foundindex = i;
break;
}
}
// now you have the found index in foundindex
如果您的数组已排序,则可以使用二进制搜索,但由于您要求进行线性搜索,因此 tghis 应该可以解决问题。
于 2012-04-02T12:49:22.817 回答