Objective-c 中的 for 循环 - 原始数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/626408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:13:37  来源:igfitidea点击:

for loop in objective-c - primitive array

objective-cfor-loopprimitive

提问by sfossen

int matrix[3][3] = {
      {1,2,3},
      {1,2,3},
      {1,2,3},
}

How can I loop over it?

我怎样才能循环它?

Basically the length operation is my concern.

基本上长度操作是我关心的。

for (int i=0; XXXXX; i++) {
  for (int j=0; XXXX; j++) {
      int value = matrix[i][j];
 }
}

EDIT: Is there a dynamic way of getting the array size? Something like sizeof()?

编辑:是否有获取数组大小的动态方法?像sizeof()什么?

回答by Adam Wright

For statically created array types, you can use the sizeof operator, e.g.

对于静态创建的数组类型,您可以使用 sizeof 运算符,例如

sizeof(myArray) / sizeof(myArray[0])

For dynamically created arrays (i.e referened by pointer), this won't work (sizeof will just give you the size of a pointer on your system). In this case, you need either constants, or a sentinal value in your array. With a sentinal, just scan each axis until you find it for the length (this is how C strings work, using \0).

对于动态创建的数组(即由指针引用),这不起作用(sizeof 只会为您提供系统上指针的大小)。在这种情况下,您需要数组中的常量或标记值。使用哨兵,只需扫描每个轴,直到找到它的长度(这就是 C 字符串的工作方式,使用 \0)。

回答by sfossen

In C I'd do the following, try:

在 C 中,我会执行以下操作,请尝试:

sizeof( matrix ) /sizeof( matrix[0] )         <- outer array
sizeof( matrix[0] )/ sizeof( matrix[0][0] )   <- inner array



linux ~ $ cat sizeof_test.c
#include <stdio.h>

int main( void )
{
        int matrix[][3] = { {1,2,3}, {1,2,3}, {1,2,3}, };
        int matrix2[][3] = { {1,2,3}, {1,2,3}, {1,2,3}, {1,2,3}, };
        int matrix3[][4] = { {1,2,3,4}, {1,2,3,4}, {1,2,3,4}, {1,2,3,4}, };

        printf( "array (%d) - elements( %d )\n", sizeof( matrix ) /sizeof( matrix[0] ), sizeof( matrix[0] )/ sizeof( matrix[0][0] ));
        printf( "array (%d) - elements( %d )\n", sizeof( matrix2 ) /sizeof( matrix2[0] ), sizeof( matrix2[0] )/ sizeof( matrix2[0][0] ));
        printf( "array (%d) - elements( %d )\n", sizeof( matrix3 ) /sizeof( matrix3[0] ), sizeof( matrix3[0] )/ sizeof( matrix3[0][0] ));

        return 0;
}
linux ~ $ gcc sizeof_test.c -o sizeof_test
linux ~ $ ./sizeof_test
array (3) - elements( 3 )
array (4) - elements( 3 )
array (4) - elements( 4 )
linux ~ $

回答by Sean

You can do this just as you would in C:

您可以像在 C 中那样执行此操作:

int matrix[3][3] = { {1,2,3}, {1,2,3}, {1,2,3}, };

for(int i = 0; i < 3; i++)
{
    for(int j = 0; j < 3; j++)
    {
        int value = matrix[i][j];
    }
}

Though, I recommend using a constant instead of magic 3's. Will make everything more readable, especially the for loop.

不过,我建议使用常量而不是魔术 3。将使所有内容更具可读性,尤其是 for 循环。

回答by drewh

The length of the top level array is 3, the length of each sub array is 3, so this should work:

顶级数组的长度为 3,每个子数组的长度为 3,所以这应该可以工作:

for( int i = 0; i < 3; ++i ) {
      for( int j = 0; j < 3; ++j ) {
           // do something here for each element
      }
 }