2012-06-25 28 views
1

我试图按照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; 
    } 
} 

我不明白这是怎么回事?我错过了什么?

+0

你得到什么错误?什么不工作? – pasawaya

+0

我只是不理解如何将其映射到代码中。我很确定我上面的实现是不正确的,不是吗?我只是不明白整个算法是如何工作的。 – Snowman

+0

好的。看到我下面的算法实现 – pasawaya

回答

0

尝试是这样的:

//Employee.h 
@property (nonatomic) NSInteger ID; 
@property (nonatomic, strong) NSString *name; 

//Employee.m 
@synthesize ID, name; 



//Place where you put algorithm 
#import "Employee.h" 
------------------------ 
NSMutableArray *employeeIDs = [NSArray arrayWithObjects:[NSNumber numberWithInt:123], [NSNumber numberWithInt:456], [NSNumber numberWithInt:789], nil]; 

//Creates employee objects 
Employee *employee1 = [[Employee alloc] init]; 
employee1.ID = 123; 
employee1.name = @"John Smith"; 

Employee *employee2 = [[Employee alloc] init]; 
employee2.ID = 456; 
employee2.name = @"Bob Day"; 

Employee *employee3 = [[Employee alloc] init]; 
employee3.ID = 789; 
employee3.name = @"Steve Jobs"; 

NSMutableArray *employeesArray = [NSArray arrayWithObjects:employee1, employee2, employee3, nil]; 

for (int index = 0; index <= [employeeIDs count]; index++) { 

    for(id currentEmployee in employeesArray){ 

     if(currentEmployee.ID != currentID){ 

      Employee *newEmployee = [[Employee alloc] init]; 
      newEmployee.name = [NSString stringWithFormat:@"Employee Name"]; 
      newEmployee.ID = 384; 

      [employeeIDs addObject:[NSNumber numberWithInteger:newEmployee.ID]]; 
      [employees addObject:newEmployee.name]; 

     } 
    } 
} 

希望这会有所帮助!

+0

等等,这是苹果说的相同的方式,还是这是另一种实现? – Snowman

+0

@mohabitar这是苹果说的方式。首先,它遍历员工ID并检查该ID是否与任何员工的ID匹配。如果确实如此,它会继续到下一个ID。如果不匹配,它会创建一个新员工。 – pasawaya

+0

我不明白这是如何工作的。这对每个'employeeID'每次遍历'employeesArray',所以'if(currentEmployee.ID!= currentID)'条件将会多次成立。 – ChrisH

0

你需要在你的对象数组做线性搜索来检查,如果你能找到它,也许类似的东西:

for (int i = 0; i < ids.count; i++) { 
    bool found = NO; 
    currentId = [ids objectAtIndex:i]; 

    // We need to traverse the whole array to check if we can find the objectID somewhere... 
    for(int j = 0; j < objects.count; j++) { 
     currentObject = [objects objectAtIndex:j]; 

     if (currentId == currentObject) { 
      found = YES; 
      break; 
     } 
    } 

    if (!found) 
    { 
     // Create the new object 
    } 
} 
+0

这不像Apple建议的那样高效。 – Snowman

+0

您有2个数组,没有特定的命令(我想),所以二进制搜索将不会有用。也许我没有关于你的问题的所有信息,但是从我所看到的,你需要2'for'循环来遍历第二个,使它成为O(n^2)... – allaire

+0

Ya但是没有'苹果的方法只有一个for循环? – Snowman

0

事情是这样的:

NSArray *wholeList = [[NSArray alloc]initWithObjects:@"employee1", @"employee2", @"employee3", @"employee4", @"employee5",@"employee6", nil]; 
NSArray *partialList = [[NSArray alloc]initWithObjects:@"employee2", @"employee13", @"employee7", nil]; 

for (id employeeID in partialList) //get one employee from the partialList 
{ 
    if (![wholeList containsObject:employeeID]) //check to see if it is in the wholeList 
    { 
     NSLog(@"This employee is not in the wholeList: %@", employeeID); 
     // do create new employee object for this employeeID, whatever... 
    } 
} 
+0

这并不像苹果推荐的那样高效。你有一个O(n)for循环,然后[list containsObject:]我认为它也是O(n)。从上面的方式是更有效率,我只是不知道如何实现它 – Snowman

1

在查看Core Data Programming Guide中的相同示例后,我发现此问题。

这是我的解决方案:

的关键是,你走2个数组分别,一个数组包含需要在核心数据存在的所有雇员字符串,另一个包含已经在核心存在Employee对象数据,由employeeId字符串过滤。这两个数组已经排序。

因此,可以说,我们有一个包含排序employeeIds字符串数组:

@"10",@"11",@"12",@"15",@"20" 

而且我们有一个包含有雇员10和2个Employee对象15

我们需要创建新的雇员matchingEmployees阵列对象为雇员ID为11,12和20的雇员,同时可能更新雇员10和15的属性。所以:

int i = 0; // employeeIds array index 
int j = 0; // matchingEmployees array index 

while ((i < [employeeIds count]) && (j <= [matchingEmployees count])){ 

    NSString *employeeId = [employeeIds objectAtIndex:i]; 

    Employee *employee = nil; 

    if ([matchingEmployees count]!=0) 
     employee = [matchingEmployees objectAtIndex:j]; 

    if (![employeeId isEqualToString:employee.employeeId]){ 

     employee = //Insert new Employee entity into context 
     employee.employeeId = employeeId; 

     //Set any attributes for employee that do not change 
    } 
    else { 
     //We matched employeeId to Employee so the next iteration 
     //of this loop should check the next Employee object 
     j++; 
    } 

    //Set any attributes for employee that change with each update 

    i++; 
}