我试图按照steps found here上比较两个数组,并知道何时创建新的对象,但我只是不明白它是如何工作的:基本阵列比较算法
你最终两个已排序的数组 - 其中一个员工ID将 传递到提取请求中,另一个与管理对象匹配 他们。为了处理这些,你走路以下 步骤的有序列表:
Get the next ID and Employee. If the ID doesn't match the Employee ID, create a new Employee for that ID.
Get the next Employee: if the IDs match, move to the next ID and Employee.
不管你有多少个ID的传递,你只能执行单一的 取出,剩下的就是刚会走路的结果组。
基本上发生了什么是我有一个来自外部来源的对象ID数组,而客户端系统只有这些ID代表的对象的子集。我需要弄清楚我已经拥有哪些物品,如果我没有它们,请逐个创建它们。
我不明白这是如何工作的。我无法将其转换为代码:
for (int i =0;i<ids.count;i++) {
currentId = [ids objectAtIndex:i];
currentObject = [objects objectAtIndex:i];
if(currentObject.id != currentId) {
//create new object
}
//"get the next employee"
//uh what?
nextEmployee = [objects objectAtIndex:i+1]; //?
if(nextEmployee.id == currentId) {
//"move on to the next id"
continue;
}
}
我不明白这是怎么回事?我错过了什么?
你得到什么错误?什么不工作? – pasawaya
我只是不理解如何将其映射到代码中。我很确定我上面的实现是不正确的,不是吗?我只是不明白整个算法是如何工作的。 – Snowman
好的。看到我下面的算法实现 – pasawaya