Objective C - 使用初始化时指定的尺寸创建一个多维数组

时间:2011-02-13 05:28:55

标签: objective-c arrays

我正在尝试创建一个类,其中可以使用init参数在初始化时动态创建二维数组的宽度和高度。

我一直在网上浏览几个小时,找不到办法。

使用标准C语法[][]不允许使用变量来声明数组。 在我看到的所有示例中,Objective C中的可变数组要求在创建时对对象进行硬编码。

有没有办法在对象中创建一个二维数组,并使用参数来定义创建时的大小?

希望有人能告诉我我错过了什么......

3 个答案:

答案 0 :(得分:6)

您可以通过在NSMutableArray上编写类别来轻松完成此操作:

@interface NSMutableArray (MultidimensionalAdditions) 

+ (NSMutableArray *) arrayOfWidth:(NSInteger) width andHeight:(NSInteger) height;

- (id) initWithWidth:(NSInteger) width andHeight:(NSInteger) height;

@end

@implementation NSMutableArray (MultidimensionalAdditions) 

+ (NSMutableArray *) arrayOfWidth:(NSInteger) width andHeight:(NSInteger) height {
   return [[[self alloc] initWithWidth:width andHeight:height] autorelease];
}

- (id) initWithWidth:(NSInteger) width andHeight:(NSInteger) height {
   if((self = [self initWithCapacity:height])) {
      for(int i = 0; i < height; i++) {
         NSMutableArray *inner = [[NSMutableArray alloc] initWithCapacity:width];
         for(int j = 0; j < width; j++)
            [inner addObject:[NSNull null]];
         [self addObject:inner];
         [inner release];
      }
   }
   return self;
}

@end

用法:

NSMutableArray *dynamic_md_array = [NSMutableArray arrayOfWidth:2 andHeight:2];

或者:

NSMutableArray *dynamic_md_array = [[NSMutableArray alloc] initWithWidth:2 andHeight:2];

答案 1 :(得分:3)

这是另一个纯粹的Objective C Version

#import foundation.h

@interface ZTwoDimensionalArray : NSObject{

    @package 
    NSMutableArray* _array;
    int _rows, _columns;
}

-(id) initWIthRows:(int)numberOfRows andColumns:(int) numberOfColumns;

-(id) getObjectAtRow:(int) row andColumn:(int)column;

-(void) setObject:(id) anObject atRow:(int) row andColumn:(int)column;
@end



#import "ZTwoDimensionalArray.h"

@implementation ZTwoDimensionalArray

-(id) initWIthRows:(int)numberOfRows andColumns:(int) numberOfColumns{

    if (self = [super init]) {
        _array = [NSMutableArray initWithCapacity:numberOfRows*numberOfColumns];
        _rows = numberOfRows;
        _columns = numberOfColumns;
    }
    return self;
}

-(id) getObjectAtRow:(int) row andColumn:(int)column{

    return [_array objectAtIndex: row*_rows + column];
}

-(void) setObject:(id) anObject atRow:(int) row andColumn:(int)column{

    [_array insertObject:anObject atIndex:row*_rows + column];
}

-(void) dealloc{

    [_array release];
}
@end

答案 2 :(得分:1)

这是另一种方式。当然这仅适用于int,但代码可以很容易地针对其他数据类型进行更改。

<强> AOMatrix.h:

#import <Cocoa/Cocoa.h>


@interface AOMatrix : NSObject {

@private
    int* matrix_;
    uint columnCount_;
    uint rowCount_;
}

- (id)initWithRows:(uint)rowCount Columns:(uint)columnCount;

- (uint)rowCount;
- (uint)columnCount;

- (int)valueAtRow:(uint)rowIndex Column:(uint)columnIndex;
- (void)setValue:(int)value atRow:(uint)rowIndex Column:(uint)columnIndex;

@end

<强> AOMatrix.m

#import "AOMatrix.h"

#define INITIAL_MATRIX_VALUE 0
#define DEFAULT_ROW_COUNT 4
#define DEFAULT_COLUMN_COUNT 4

/****************************************************************************
 * BIG NOTE:
 * Access values in the matrix_ by matrix_[rowIndex*columnCount+columnIndex]
 ****************************************************************************/
@implementation AOMatrix

- (id)init {
    return [self initWithRows:DEFAULT_ROW_COUNT Columns:DEFAULT_COLUMN_COUNT];
}

- (id)initWithRows:(uint)initRowCount Columns:(uint)initColumnCount {
    self = [super init];
    if(self) {
        rowCount_ = initRowCount;
        columnCount_ = initColumnCount;
        matrix_ = malloc(sizeof(int)*rowCount_*columnCount_);

        uint i;
        for(i = 0; i < rowCount_*columnCount_; ++i) {
            matrix_[i] = INITIAL_MATRIX_VALUE;
        }
//      NSLog(@"matrix_ is %ux%u", rowCount_, columnCount_);
//      NSLog(@"matrix_[0] is at %p", &(matrix_[0]));
//      NSLog(@"matrix_[%u] is at %p", i-1, &(matrix_[i-1]));
    }
    return self;
}

- (void)dealloc {
    free(matrix_);
    [super dealloc];
}

- (uint)rowCount {
    return rowCount_;
}

- (uint)columnCount {
    return columnCount_;
}

- (int)valueAtRow:(uint)rowIndex Column:(uint)columnIndex {
//  NSLog(@"matrix_[%u](%u,%u) is at %p with value %d", rowIndex*columnCount_+columnIndex, rowIndex, columnIndex, &(matrix_[rowIndex*columnCount_+columnIndex]), matrix_[rowIndex*columnCount+columnIndex]);
    return matrix_[rowIndex*columnCount_+columnIndex];
}
- (void)setValue:(int)value atRow:(uint)rowIndex Column:(uint)columnIndex {
    matrix_[rowIndex*columnCount_+columnIndex] = value;
}

@end