Objective-C的访问阵列 [英] objective-c accessing arrays

查看:131
本文介绍了Objective-C的访问阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了以下尝试创建二维和三维数组:

I have done the following trying to create 2d and 3d arrays:

Array1 = [[NSMutableArray alloc]init];
Array2 = [[NSMutableArray alloc]init];
Array3 = [[NSMutableArray alloc]init];

for loop 
[Array1 insertObject:Array2 atIndex:i];
//some code....
[Array2 insertObject:Array3 atIndex:j];

林不知道这是否是正确的,但在我的code我每次循环我添加ARRAY2到数组1的新指标,但即时通讯注意如果确定这个工程。换句话说,我希望我不是再次每次移动整个数组?!

im not sure if this is right but every time i loop in my code i add Array2 to a new index in Array1 but im note sure if this works. In other words i hope im not moving the whole array again every time ?!!

现在我的问题是,我需要通过ARRAY1循环访问数组2,然后通过数组2 ARRAY3。我只需要知道如何使用循环,这样我可以显示每个阵列的内容,以访问这些阵列。我需要做的是这样的数组[我] [J]。
 其中,i表示了array1和J是数组2

Now my problem is that i need to access array2 through looping in array1, then array3 through array2. I just need to know how to access these arrays using loops so that i can display each array's contents. I need to do something like this array [i][j] where "i" for array1 and "j" is for array2

推荐答案

创建三维阵列(NxMxP):

Create 3d-array (NxMxP):

NSMutableArray *array3D = [[NSMutableArray alloc] initWithCapacity:N];

for (int i = 0; i < N; ++i)
{
    NSMutableArray *array2D = [[NSMutableArray alloc] initWithCapacity:M];
    for (int i = 0; i < M; ++i)
    {
        NSMutableArray *justAnArray = [[NSMutableArray alloc] initWithCapacity:P];
        [array2D addObject:justAnArray];
        [justAnArray release];
    }
    [array3D addObject:array2D];
    [array2D release];
}

使用此生物:

[[[array3D objectAtIndex:3] objectAtIndex:4] objectAtIndex:1]; // it's like array3D[3][4][1]

这篇关于Objective-C的访问阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆