基本数组比较算法 [英] Basic array comparison algorithm

查看:152
本文介绍了基本数组比较算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想遵循在这里找到的步骤比较两个数组,并知道什么时候创建一个新的对象,但我只是不明白它是如何工作的:


你最终得到两个排序的数组 - 一个是雇员ID将
传递给fetch请求,另一个使用与
匹配的managed对象。要处理它们,您可以按照这些
步骤操作排序的列表:




 下一个ID和员工。如果ID与员工ID不匹配,请为该ID创建一个新员工。 
获取下一个Employee:如果ID匹配,移动到下一个ID和Employee。




无论您传入多少ID,
fetch,其余只是走结果集。


基本上发生了什么是我有一个对象id数组,并且客户端系统仅具有由这些ID表示的对象的子集。我需要弄清楚我已经有了哪些对象,如果我没有对象,就一个一个地创建它们。



我不明白这是如何工作的。将此代码转换为代码时遇到问题:

  for(int i = 0; i< ids.count; i ++){ 
currentId = [ids objectAtIndex:i];
currentObject = [object objectAtIndex:i];

if(currentObject.id!= currentId){
//创建新对象
}

//获取下一个员工
//呃什么?
nextEmployee = [object objectAtIndex:i + 1]; //?
if(nextEmployee.id == currentId){
//移动到下一个id
continue;
}
}

我不明白这将如何工作?


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

//Employee.m
@synthesize ID,name;



//放置算法的位置
#importEmployee.h
------------ ------------
NSMutableArray * employeeIDs = [NSArray arrayWithObjects:[NSNumber numberWithInt:123],[NSNumber numberWithInt:456],[NSNumber numberWithInt:789],nil];

//创建员工对象
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];

}
}
}

帮助!


I'm trying to follow the steps found here on comparing two arrays, and knowing when to create a new object, but I just don't understand how it works:

You end up with two sorted arrays—one with the employee IDs passed into the fetch request, and one with the managed objects that matched them. To process them, you walk the sorted lists following these steps:

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.

Regardless of how many IDs you pass in, you only execute a single fetch, and the rest is just walking the result set.

Basically what's happening is I have an array of object ids from an external source, and the client system only has a subset of the objects represented by these ids. I need to figure out which objects I already have, and if I don't have them, create them, one by one.

I don't understand how this works. I'm having trouble translating this to code:

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;
    }
}

I don't see how that would work? What am I missing?

解决方案

Try something like this:

//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];

        }
    }
}

Hope this helps!

这篇关于基本数组比较算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆