Objective-C 访问数组

发布于 2024-11-05 09:06:58 字数 515 浏览 0 评论 0原文

我已经做了以下尝试创建 2d 和 3d 数组:

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];

我不确定这是否正确,但每次我在代码中循环时,我都会将 Array2 添加到 Array1 中的新索引,但我注意到这是否有效。换句话说,我希望我不会每次都再次移动整个阵列?!!

现在我的问题是我需要通过 array1 循环访问 array2,然后通过 array2 访问 array3。我只需要知道如何使用循环访问这些数组,以便我可以显示每个数组的内容。我需要做类似数组 [i][j] 的事情 其中“i”代表数组 1,“j”代表数组 2

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];

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 ?!!

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你列表最软的妹 2024-11-12 09:06:58

创建 3d 阵列 (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]

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];
}

Use this creature:

[[[array3D objectAtIndex:3] objectAtIndex:4] objectAtIndex:1]; // it's like array3D[3][4][1]
魔法唧唧 2024-11-12 09:06:58

只写我所理解的...

for 访问循环中的数组
for循环

 NSArray arr = [Array1 objectAtIndex:i] objectIndex:j]; 

将给出arr[i][j];

Just writing what I have understood...

for accessin the array in loop do
for loop

 NSArray arr = [Array1 objectAtIndex:i] objectIndex:j]; 

will give the the arr[i][j];

阳光①夏 2024-11-12 09:06:58

我不确定我是否正确理解你,但如果你想访问数组中的数组,你可以使用

    [[array1 objectAtIndex:i] objectAtIndex:j];

请完成这个SO问题如何在 iPhone 中创建数组的数组?

您需要这样的东西吗?

更新

NSMutableArray *array1 = [NSMutableArray arrayWithObjects:@"A",@"B",@"C",nil];
NSMutableArray *array2 = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",nil];
NSMutableArray *array3 = [[[NSMutableArray alloc] init] autorelease];
[array3 addObject:array2];
[array3 addObject:array1];
for(int i = 0; i < [array3 count]; i++)
  for(int j = 0; j<[array1 count]; j++)
    NSLog(@"From array3 %@",[[array3 objectAtIndex:i] objectAtIndex:j]);

I am not sure if i understand you properly but if you want to access an array within an array you can use

    [[array1 objectAtIndex:i] objectAtIndex:j];

Pls go through this SO question How to create Array of Array in iPhone?

Is something like this what you need?

UPDATE

NSMutableArray *array1 = [NSMutableArray arrayWithObjects:@"A",@"B",@"C",nil];
NSMutableArray *array2 = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",nil];
NSMutableArray *array3 = [[[NSMutableArray alloc] init] autorelease];
[array3 addObject:array2];
[array3 addObject:array1];
for(int i = 0; i < [array3 count]; i++)
  for(int j = 0; j<[array1 count]; j++)
    NSLog(@"From array3 %@",[[array3 objectAtIndex:i] objectAtIndex:j]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文