如下代码,我想将 matrix1Col 和 matrix2Col 发送到 multiply 方法。
但索引 1 超出范围是错误的。
从 UIButton 代码内部
double kk[2][2] = {{1,2},{5,6}};
double t [2][1] = {1,2};`
if (!matrix1Col) {
matrix1Col = [NSMutableArray array];
}
for (unsigned int i=0; i<2; i++) {
matrix1Row = [NSMutableArray new];
for (unsigned int j=0 ; j<2; j++) {
[matrix1Row addObject:@(kk[i][j])];
}
[matrix1Col addObject:matrix1Row];
}
if (!matrix2Col) {
matrix2Col = [NSMutableArray array];
}
for (unsigned int i=0; i<2; i++) {
matrix2Row = [NSMutableArray new];
for (unsigned int j=0; j<1; j++) {
[matrix2Row addObject:@(t[i][j])];
}
[matrix2Col addObject:matrix2Row];
}
NSMutableArray *resultMultiply = [self multiply:matrix1Col :matrix2Col];
ViewController.m 中的另一个方法:
-(NSMutableArray*)multiply:(NSMutableArray*)matrix1 :(NSMutableArray*)matrix2{
int matrix1RowCount = [matrix1 count];
int matrix2RowCount = [matrix2 count];
int matrix2ColCount = [[matrix2 objectAtIndex:0] count];
NSMutableArray *multiplyMatrix = [NSMutableArray arrayWithCapacity:matrix1RowCount];
for (int i=0; i< matrix1RowCount; i++) {
NSMutableArray *matrixInRow = [NSMutableArray arrayWithCapacity:matrix2ColCount];
for (int j=0; j<matrix2RowCount; j++) {
double valueTotal = 0;
for (int k=0; k<matrix2ColCount; k++) {
double value1 = [[[matrix1 objectAtIndex:i] objectAtIndex:k] doubleValue];
double value2 = [[[matrix2 objectAtIndex:k] objectAtIndex:j] doubleValue];
valueTotal += value1*value2;
}
[matrixInRow addObject:[NSNumber numberWithDouble:valueTotal]];
}
[multiplyMatrix addObject:matrixInRow];
}
return multiplyMatrix;
}
这是错误代码:
2013-02-14 06:26:06.595 matrixMutableArray[21578:c07] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
*** First throw call stack:
我找不到错误的地方。
谢谢你的评论。