multidimensional array

余生颓废 提交于 2019-12-25 03:58:24

问题


Im looking for an example of multidimensional arrays. I have an array of thumbtails (9 for example) and a tablecellview of 4 thumbs pre row, giving me 3 rows. I want to create a new multidimensional array which will hold the 3 rows each row containing 4 arrays.

Ive looked at many example for the past 3h but they all seem to suggest to use C-style coding and im not sure how to go about initialising them or if i need to release. Plus im using this in a tableview so im not sure if ill need to use NSarray or ill be able to get away with C-style array. Any suggestions gratefully appreciated.

thumbnailarr[0][0] = 'img1.png';
thumbnailarr[0][1] = 'img2.png';
thumbnailarr[0][2] = 'img3.png';
thumbnailarr[0][3] = 'img4.png';

thumbnailarr[1][0] = 'img5.png';
thumbnailarr[1][1] = 'img6.png';
thumbnailarr[1][2] = 'img7.png';
thumbnailarr[1][3] = 'img8.png';

thumbnailarr[2][0] = 'img9.png';

回答1:


A multidimensional array is essentially an array of arrays, an NSArray can have NSArrays as its contents. For example:

NSArray *thumbs= [NSArray arrayWithObjects:
                          [NSArray arrayWithObjects: @"img1.png",@"img2.png",@"img3.png",@"img4.png",nil],
                          [NSArray arrayWithObjects: @"img5.png",@"img6.png",@"img7.png",@"img8.png",nil],
                          [NSArray arrayWithObject: @"img9.png"],nil];

access like this:

[[thumbs objectAtIndex:i] objectAtIndex:j]; //same as thumbs[i][j]



回答2:


There is no special multidimensional array in objective-C but you could work with a one-dimensional array, too.

You would then use modulo-based calculations to convert the desired row and column index to the array index: You would use an NSIndexPath to describe the "coordinates" in your multidimensional array.

NSUInteger nRows = 4;
NSUInteger nCols = 3;

-(NSInteger)indexForIndexPath:(NSIndexPath *)indexPath
{
    // check if the indexpath is correct with two elements (row and col)
    if ([indexPath length]!= 2) return -1;
    NSUIntegers indexes[2];
    [indexPath getIndexes:indexes];
    return indexes[0]*nCols+indexes[1];
}

-(NSIndexPath *)indexPathForIndex:(NSInteger)index
{
    NSInteger indexes[2];
    NSInteger indexes[0] = index/nCols;
    NSInteger indexes[1] = index%nCols;
    return [NSIndexPath indexPathWithIndexes:indexes length:2]
}



回答3:


Objective-C does not have anything special for multidimentional array. You need to use C 2D array, unless you want to use NSArray of NSArray.

NSString *thumbnailarr[3][4];

// initialize is easy if you include row-column in image names
// like img10.png instead of img5.png, img01.png instead of img2.png
for (NSInteger i = 0; i < 3; i++) {
    for (NSInteger j = 0; j < 4; j++) {
        thumbnailarr[i][j] = [[NSString alloc] initWithFormat:@"img%d%d.png", i, j];
    }
}

// in dealloc release them
for (NSInteger i = 0; i < 3; i++) {
    for (NSInteger j = 0; j < 4; j++) {
        [thumbnailarr[i][j] release];
    }
}

And for the table view, row count is what you return from the tableView:numberOfRowsInSection: method. It does not matter whether you return a NSArray count or hard coded integer. That means if you return 3 from this method then there will be 3 cells. There is no special dependency on NSArray.



来源:https://stackoverflow.com/questions/5563692/multidimensional-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!