• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 不同 NSArray 对象的组合

[复制链接]
菜鸟教程小白 发表于 2022-12-13 05:30:26 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我想在不同的 arrays 中找到 elements 的组合。假设我有三个 NSArray 对象:

NSArray *set1 = [NSArray arrayWithObjects"A",@"B",@"C", nil];
NSArray *set2 = [NSArray arrayWithObjects"a",@"b", nil];
NSArray *set3 = [NSArray arrayWithObjects"1",nil];

现在需要的答案是以下数组

NSArray *combinations = [{A},{B},{C},{a},{b},{1},{A,a},{A,b},{A,1},{B,a},{B,b},{B,1},{a,1},{b,1},{A,a,1},{A,b,1},{B,a,1},{B,b,1},{C,a,1},{C,b,1}];

编辑 目前我已经做了以下代码,我能够得到两个长度的组合。

    NSArray *set1 = [NSArray arrayWithObjects"A",@"B",@"C", nil];
    NSArray *set2 = [NSArray arrayWithObjects"a",@"b", nil];
    NSArray *set3 = [NSArray arrayWithObjects"1",nil];

    NSArray *allSets = [NSArray arrayWithObjects:set1,set2,set3,nil];
    NSMutableArray *combinations = [NSMutableArray new];

    for (int index = 0; index < allSets.count; index++) {
        [combinations addObject:[NSMutableArray array]];
    }

    NSMutableArray *singleCombinations = combinations[0];

    for (NSArray *set in allSets) {
        [singleCombinations addObjectsFromArray:set];
    }

    for (int outerIndex = 0; outerIndex < allSets.count-1; outerIndex++) {

        NSArray *set = allSets[outerIndex];

        for (id object1 in set) {

            for (int innerIndex = outerIndex+1; innerIndex<allSets.count; innerIndex++) {
                NSArray *nextSet = allSets[innerIndex];

                for (id object2 in nextSet) {
                    NSString *combi = [NSString stringWithFormat"%@%@",object1,object2];
                    NSLog(@"%@",combi);
                }

            }

        }

    }

有什么帮助???



Best Answer-推荐答案


使用以下函数,它追加 a2 的所有元素到 a1 的每个元素:

NSArray *combinations(NSArray *a1, NSArray *a2)
{
    NSMutableArray *result = [NSMutableArray array];
    for (NSArray *elem1 in a1) {
        [result addObject:elem1];
        for (id elem2 in a2) {
            [result addObject:[elem1 arrayByAddingObject:elem2]];
        }
    }
    return result;
}

你可以从一个空数组开始迭代得到结果,然后 将其与您的套装相结合:

NSArray *set1 = @[@"A", @"B", @"C"];
NSArray *set2 = @[@"a", @"b"];
NSArray *set3 = @[@"1"];

NSArray *result = @[@[]];
result = combinations(result, set1);
result = combinations(result, set2);
result = combinations(result, set3);

显示结果:

for (NSArray *item in result) {
    NSLog(@"{ %@ }", [item componentsJoinedByString", "]);
}

输出

{ }
{ 1 }
{ 一个 }
{一个,1}
{乙}
{ b, 1 }
{ 一个 }
{一,1}
{一个,一个}
{ 一个,一个,1 }
{一,乙}
{ A, b, 1 }
{乙}
{乙,1}
{乙,一个}
{ 乙, 一, 1 }
{乙,乙}
{ 乙,乙,1 }
{ C }
{ C, 1 }
{ C, 一个 }
{ C, 一, 1 }
{ C, b }
{ C, b, 1 }

关于ios - 不同 NSArray 对象的组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20048824/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap