如何创建2D数组目标C.

时间:2021-01-18 21:32:13

I have 2 arrays that were created dynamically. I have read some examples on the net, and I could not understand it fully. What I see, is that the arrays seems to be 1 Dimension and not 2.

我有2个动态创建的数组。我在网上看过一些例子,我完全听不懂。我所看到的是,数组似乎是1维而不是2维。

The codes below assigns some objects to array "combineObjectIssues", which will then be added into "combineAll" to get a 2D-Array. I want "currentObject.date" to be index 0, while "issuesDiscovered" array to be index 1.

下面的代码将一些对象分配给数组“combineObjectIssues”,然后将其添加到“combineAll”以获取2D数组。我希望“currentObject.date”为索引0,而“issuesDiscovered”数组为索引1。

for (currentObject in currentObjects) {
        [combineObjectIssues addObject:currentObject.date]; //2D Array Row
        for (Issue *checkIssue in currentObject.issuesDiscovered) {
            if (checkIssue) {
                [issuesDiscovered addObject:checkIssue];
            }
        }
        [tempIssues addObject:[issuesDiscovered copy]]; // to combine all array of issues
        [combineOjectIssues addObjectsFromArray:[issuesDiscovered copy]]; //2D Array column
        [combineAll addObject:[combineObjectIssues copy]];
        [issuesDiscovered removeAllObjects]; //remove all objects;
        [combineObjectIssues removeAllObjects]; //remove all objects
    }
}

Below is my output for combineAll array.

下面是我对combineAll数组的输出。

(
    (
    "2013-07-19 09:00:00",
    "<Issue: 0x8c171f0>",
    "<Issue: 0x8c16e50>",
    "<Issue: 0x8c16d30>",
    "<Issue: 0x8c16a10>",
    "<Issue: 0x8c16090>",
    "<Issue: 0x8c15bb0>",
    "<Issue: 0x8c156d0>"
),
    (
    "2013-07-13 14:30:00"
),
    (
    "2013-06-08 14:30:00",
    "<Issue: 0x8c10340>",
    "<Issue: 0x8c0fad0>",
    "<Issue: 0x8c0f590>",
    "<Issue: 0x8c0f0c0>"
),
    (
    "2013-05-04 11:30:00"
)
)

As you can see from the output, its a 1 dimension array, which I do not want. I want to have something like, for [0][0], it contains the date, whereby for [0][1], it contains an array of issues.

从输出中可以看出,它是一个1维数组,我不想要。我希望有类似的东西,对于[0] [0],它包含日期,对于[0] [1],它包含一系列问题。

I know my codes might not be right. As such, kindly assist me. Your assistance(s) are appreciated.

我知道我的代码可能不对。因此,请帮助我。您的帮助表示赞赏。

3 个解决方案

#1


2  

Try this:

you can always get like that values but syntax can be different.

你可以随时得到这些值,但语法可能会有所不同。

[[combineAll objectAtIndex:0] objectAtIndex:1];

this way you can get the value for [0][1]. you can do like this to make it 2 dimensional array.

这样你就可以获得[0] [1]的值。你可以这样做,使它成为二维数组。

also you can do this:

你也可以这样做:

NSString* str = combineAll[0][1];

For all the issue data you can do this:

对于所有问题数据,您可以执行以下操作:

NSArray* issueArray = combineAll[0];

this will return all the issues at position 0 of combineAll array.

这将返回combineAll数组的位置0处的所有问题。

Hope it Helps!!

希望能帮助到你!!

#2


1  

The question of multidimensional data structures to store Cocoa objects comes up frequently. Sometimes it can be solved by re-thinking your object hierarchy in a way that better fits the conceptual model. But sometimes a multidimensional array is the best mechanism. Ordinary C arrays can store pointers to Cocoa objects, and may be a better way of dealing with this. By way of illustration:

存储Cocoa对象的多维数据结构的问题经常出现。有时可以通过以更好地适合概念模型的方式重新思考对象层次结构来解决它。但有时多维数组是最好的机制。普通的C数组可以存储指向Cocoa对象的指针,也许是处理这个问题的更好方法。举例说明:

#import <Foundation/Foundation.h>

@interface Foo : NSObject
@property (nonatomic,strong) NSString *name;
@end

@implementation Foo
@end

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        id p[10][10];
        //  create a bunch of foos
        for( uint8_t i = 0; i < 10; i++ ) {
            for( uint8_t j = 0; j < 10 ; j++ ) {
                Foo *aFoo = [[Foo alloc] init];
                aFoo.name = [NSString stringWithFormat:@"Foo_%02d_%02d",i,j];
                p[i][j] = aFoo;
            }
        }
        //  show that we can recover a Foo from C array
        Foo *someFoo = (Foo *)p[5][5];
        NSLog(@"Foo[5][5] = %@",someFoo.name);
        // prints Foo[5][5] = Foo_05_05 to the console
    }
    return 0;
}

Or, if you want to allocate the C array dynamically, you'll have little more work to do. See this gist

或者,如果要动态分配C数组,您将无需再做多少工作。看到这个要点

#3


0  

You're going to make life hard on yourself by using this kind of data structure. You'll always have to remember that the first index is a different type than the rest of them, and always having to do index shifting and so forth. But if you must, try something like this:

你将通过使用这种数据结构来改善自己的生活。你总是要记住第一个索引是与其他索引不同的类型,并且总是必须进行索引转换等等。但如果你必须,尝试这样的事情:

NSMutableArray *output = [NSMutableArray array];
for (YourClass *currentObject in currentObjects) {
    //Making assumption currentObject.issuesDiscovered in an NSArray! if it's not you'll need to initialize the second element differently
    NSArray *arrayForObject = @[currentObject.date,[currentObject.issuesDiscovered copy]];
    [output addObject:arrayForObject];
}

#1


2  

Try this:

you can always get like that values but syntax can be different.

你可以随时得到这些值,但语法可能会有所不同。

[[combineAll objectAtIndex:0] objectAtIndex:1];

this way you can get the value for [0][1]. you can do like this to make it 2 dimensional array.

这样你就可以获得[0] [1]的值。你可以这样做,使它成为二维数组。

also you can do this:

你也可以这样做:

NSString* str = combineAll[0][1];

For all the issue data you can do this:

对于所有问题数据,您可以执行以下操作:

NSArray* issueArray = combineAll[0];

this will return all the issues at position 0 of combineAll array.

这将返回combineAll数组的位置0处的所有问题。

Hope it Helps!!

希望能帮助到你!!

#2


1  

The question of multidimensional data structures to store Cocoa objects comes up frequently. Sometimes it can be solved by re-thinking your object hierarchy in a way that better fits the conceptual model. But sometimes a multidimensional array is the best mechanism. Ordinary C arrays can store pointers to Cocoa objects, and may be a better way of dealing with this. By way of illustration:

存储Cocoa对象的多维数据结构的问题经常出现。有时可以通过以更好地适合概念模型的方式重新思考对象层次结构来解决它。但有时多维数组是最好的机制。普通的C数组可以存储指向Cocoa对象的指针,也许是处理这个问题的更好方法。举例说明:

#import <Foundation/Foundation.h>

@interface Foo : NSObject
@property (nonatomic,strong) NSString *name;
@end

@implementation Foo
@end

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        id p[10][10];
        //  create a bunch of foos
        for( uint8_t i = 0; i < 10; i++ ) {
            for( uint8_t j = 0; j < 10 ; j++ ) {
                Foo *aFoo = [[Foo alloc] init];
                aFoo.name = [NSString stringWithFormat:@"Foo_%02d_%02d",i,j];
                p[i][j] = aFoo;
            }
        }
        //  show that we can recover a Foo from C array
        Foo *someFoo = (Foo *)p[5][5];
        NSLog(@"Foo[5][5] = %@",someFoo.name);
        // prints Foo[5][5] = Foo_05_05 to the console
    }
    return 0;
}

Or, if you want to allocate the C array dynamically, you'll have little more work to do. See this gist

或者,如果要动态分配C数组,您将无需再做多少工作。看到这个要点

#3


0  

You're going to make life hard on yourself by using this kind of data structure. You'll always have to remember that the first index is a different type than the rest of them, and always having to do index shifting and so forth. But if you must, try something like this:

你将通过使用这种数据结构来改善自己的生活。你总是要记住第一个索引是与其他索引不同的类型,并且总是必须进行索引转换等等。但如果你必须,尝试这样的事情:

NSMutableArray *output = [NSMutableArray array];
for (YourClass *currentObject in currentObjects) {
    //Making assumption currentObject.issuesDiscovered in an NSArray! if it's not you'll need to initialize the second element differently
    NSArray *arrayForObject = @[currentObject.date,[currentObject.issuesDiscovered copy]];
    [output addObject:arrayForObject];
}