【问题标题】:Separate array into new arrays with similar objects - iOS将数组分成具有相似对象的新数组 - iOS
【发布时间】:2014-01-21 01:22:28
【问题描述】:

我有一个数组:包含对象的数组 A

"Anchorage, AK"
"Juneau, AK"
"Los Angeles, CA"
"Minneapolis, MS"
"Seatac, WA"
"Seattle, WA"

注意:实际的数组对象不包含引号。

如何根据字符串的最后一个字符将此数组分成多个数组?数组对我来说是否可变并不重要。

即...

Array 2 {
[1] <--- First NSArray
"Anchorage, AK"
"Jeneau, AK"

[2] <--- Second NSArray
"Los Angeles, CA"

[3] <--- Third NSArray
"Minneapolis, MS"

[4] <--- Fourth NSArray
"Seatac, WA"
"Seattle, WA"
}

在真实场景中,我不知道每个州有多少。我在想我可以在结尾处对字符串的两个字符长度部分做些什么?因为这就是我希望将它们分成本质上的状态。

【问题讨论】:

  • 尝试实现一些东西并发布非工作代码。我们不能只为你写。
  • 你想要分割数组的标准是什么?
  • @JoshCaswell 我想根据字符串对象中的最后两个字符对对象进行分组。
  • 为什么要一个数组数组?您应该有一个字典,其中键是州,值是州的城市数组。

标签: ios objective-c arrays nsmutablearray nsarray


【解决方案1】:

好的 - 让我知道这是否清楚。

// Setup the inital array
NSArray *array = [[NSArray alloc] initWithObjects:@"Anchorage, AK",
                  @"Juneau, AK",
                  @"Los Angeles, CA",
                  @"Minneapolis, MS",
                  @"Seatac, WA",
                  @"Seattle, WA", nil];

// Create our array of arrays
NSMutableArray *newArray2 = [[NSMutableArray alloc] init];

// Loop through all of the cities using a for loop
for (NSString *city in array) {
    // Keep track of if we need to creat a new array or not
    bool foundCity = NO;

    // This gets the state, by getting the substring of the last two letters
    NSString *state = [city substringFromIndex:[city length] -2];

    // Now loop though our array of arrays tosee if we already have this state
    for (NSMutableArray *subArray in newArray2) {
        //Only check the first value, since all the values will be the same state
        NSString *arrayCity = (NSString *)subArray[0];
        NSString *arrayState = [arrayCity substringFromIndex:[arrayCity length] -2];

        if ([state isEqualToString:arrayState])
        {
            // Check if the states match... if they do, then add it to this array
            foundCity = YES;
            [subArray addObject:city];

            // No need to continue the for loop, so break stops looking though the arrays.
            break;
        }
    }

    // WE did not find the state in the newArray2, so create a new one
    if (foundCity == NO)
    {
        NSMutableArray *newCityArray = [[NSMutableArray alloc] initWithObjects:city, nil];
        [newArray2 addObject:newCityArray];
    }


}

//Print the results
NSLog(@"%@", newArray2);

我的输出

2014-01-20 20:28:04.787 TemperatureConverter[91245:a0b] (
        (
        "Anchorage, AK",
        "Juneau, AK"
    ),
        (
        "Los Angeles, CA"
    ),
        (
        "Minneapolis, MS"
    ),
        (
        "Seatac, WA",
        "Seattle, WA"
    )
)

【讨论】:

  • 但是如果我不知道每个州有多少我该怎么办?
  • 我不明白你的目标,你到底想如何分解数组?你遵循什么算法?第一个和最后一个数组是 2,其余的是 1?
  • 我已经澄清了我的问题
  • 为什么 Anchorage 和 Seatac 重复两次?
  • 因为我有 [subArray addObject:arrayCity]; 而不是 [subArray addObject:city]; - 已修复。
【解决方案2】:

您可以遍历原始数组和split them by delimiters 中的字符串并将它们放入新数组中。然后你可以根据数组的第二个元素查看数组和分组。

【讨论】:

  • 您能否详细说明如何根据第二个元素对它们进行分组?我有一个包含上述数组中所有两个字符状态名称的数组。
猜你喜欢
  • 2016-04-18
  • 1970-01-01
  • 2020-07-03
  • 2014-05-10
  • 1970-01-01
  • 2016-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多