在Objective-C中创建二维数组

时间:2010-12-06 02:41:56

标签: objective-c

在Objective-C中声明二维数组的最简单方法是什么?我正在从网站的文本文件中读取数字矩阵,并希望获取数据并将其放入3x3矩阵中。

一旦我将URL读入字符串,我创建一个NSArray并使用componentsSeparatedByString方法去除回车换行符并创建每个单独的行。然后,我得到新数组中行数的计数,以获得每行的各个值。这将为mw提供一个包含字符串的数组,而不是一行包含三个单独的值。我只需要能够获取这些值并创建一个二维数组。

6 个答案:

答案 0 :(得分:46)

如果它不需要是一个对象,你可以使用:

float matrix[3][3];

定义3x3浮点数组。

答案 1 :(得分:43)

您可以使用Objective C样式数组。

NSMutableArray *dataArray = [[NSMutableArray alloc] initWithCapacity: 3];

[dataArray insertObject:[NSMutableArray arrayWithObjects:@"0",@"0",@"0",nil] atIndex:0];
[dataArray insertObject:[NSMutableArray arrayWithObjects:@"0",@"0",@"0",nil] atIndex:1];
[dataArray insertObject:[NSMutableArray arrayWithObjects:@"0",@"0",@"0",nil] atIndex:2];

我希望你从上面的例子中得到答案。

干杯, Raxit

答案 2 :(得分:16)

这也有效:

    NSArray *myArray = @[
                            @[ @1, @2, @3, @4],
                            @[ @1, @2, @3, @4],
                            @[ @1, @2, @3, @4],
                            @[ @1, @2, @3, @4],
                       ];

在这种情况下,它是一个只有数字的4x4数组。

答案 3 :(得分:5)

我并不完全确定你在寻找什么,但我对二维数组的处理方法是创建一个新类来封装它。注意,以下内容直接输入StackOverflow应答框,因此无法编译或测试。

@interface TwoDArray : NSObject
{
@private
    NSArray* backingStore;
    size_t numRows;
    size_t numCols;
}

// values is a linear array in row major order
-(id) initWithRows: (size_t) rows cols: (size_t) cols values: (NSArray*) values;
-(id) objectAtRow: (size_t) row col: (size_t) col;

@end

@implementation TwoDArray


-(id) initWithRows: (size_t) rows cols: (size_t) cols values: (NSArray*) values
{
    self = [super init];
    if (self != nil)
    {
        if (rows * cols != [values length])
        {
            // the values are not the right size for the array
            [self release];
            return nil;
        }
        numRows = rows;
        numCols = cols;
        backingStore = [values copy];
    }
    return self;
}

-(void) dealloc
{
    [backingStore release];
    [super dealloc];
}

-(id) objectAtRow: (size_t) row col: (size_t) col
{
    if (col >= numCols)
    {
        // raise same exception as index out of bounds on NSArray.  
        // Don't need to check the row because if it's too big the 
        // retrieval from the backing store will throw an exception.
    }
    size_t index = row * numCols + col;
    return [backingStore objectAtIndex: index];
}

@end

答案 4 :(得分:1)

首先,你要在.h文件中设置一个NSMutableDictionary

            @interface MSRCommonLogic : NSObject
            {
                NSMutableDictionary *twoDimensionArray;
            }

            then have to use following functions in .m file


            - (void)setValuesToArray :(int)rows cols:(int) col value:(id)value
            {
                if(!twoDimensionArray)
                {
                    twoDimensionArray =[[NSMutableDictionary alloc]init];
                }

                NSString *strKey=[NSString stringWithFormat:@"%dVs%d",rows,col];
                [twoDimensionArray setObject:value forKey:strKey];

            }

            - (id)getValueFromArray :(int)rows cols:(int) col
            {
                NSString *strKey=[NSString stringWithFormat:@"%dVs%d",rows,col];
                return  [twoDimensionArray valueForKey:strKey];
            }


            - (void)printTwoDArray:(int)rows cols:(int) cols
            {
                NSString *strAllsValuesToprint=@"";
                strAllsValuesToprint=[strAllsValuesToprint stringByAppendingString:@"\n"];
                for (int row = 0; row < rows; row++) {
                    for (int col = 0; col < cols; col++) {

                        NSString *strV=[self getValueFromArray:row cols:col];
                        strAllsValuesToprint=[strAllsValuesToprint stringByAppendingString:[NSString stringWithFormat:@"%@",strV]];
                        strAllsValuesToprint=[strAllsValuesToprint stringByAppendingString:@"\t"];
                    }
                    strAllsValuesToprint= [strAllsValuesToprint stringByAppendingString:@"\n"];
                }

                NSLog(@"%@",strAllsValuesToprint);

            }

答案 5 :(得分:1)

希望这会有所帮助。 这只是如何在代码中初始化2d int数组(Objective C works)

的示例
int **p;
p = (int **) malloc(Nrow*sizeof(int*));
for(int i =0;i<Nrow;i++)
{
    p[i] = (int*)malloc(Ncol*sizeof(int));
}
//put something in
for(int i =0;i<Nrow;i++)
{
    p[i][i] = i*i;
    NSLog(@" Number:%d   value:%d",i, p[i][i]);
}

//free pointer after use
for(int i=0;i<Nrow;i++)
{
    p[i]=nil;
    //free(p[i]);
    NSLog(@" Number:%d",i);
}
//free(**p);
p = nil;