美文网首页
Objective-C 二维数组详解(转)

Objective-C 二维数组详解(转)

作者: 写代码的八爪鱼 | 来源:发表于2017-11-15 14:25 被阅读222次

在实际的项目开发中,二维数组也是常常用到的数据结构。OC中的二维数组也是通过一维数组来建立的,今天我们来详解一下如何在OC中使用二维数组。

【使用NSArray初始化二维数组】

使用NSArray初始化的一维数组和二维数组都是不可变数组。

[objc]

view plain

copy

<embed id="ZeroClipboardMovie_1" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="29" height="15" name="ZeroClipboardMovie_1" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=1&width=29&height=15" wmode="transparent">

print

?

  1. import <Foundation/Foundation.h>

  2. int

    main(

    int

    argc,

    const

    char

    char

    • argv[]) {
  3. @autoreleasepool

    {

  4. //定义2个一维数组;

  5. NSArray

    *firstRow = [[NSArray

    alloc

    ]

    initWithObjects

    :

    @"1"

    ,

    @"2"

    ,

    @"3"

    ,

    nil

    nil

    ];

  6. NSArray

    *secondRow = [[NSArray

    alloc

    ]

    initWithObjects

    :

    @"4"

    ,

    @"5"

    ,

    @"6"

    ,

    nil

    nil

    ];

  7. //使用一维数组来初始化二维数组;

  8. NSArray

    *my

2

DArray = [[NSArray

 alloc

]

 initWithObjects

:firstRow,secondRow,

 nil

 nil

];
  1. //输出二维数组对象;

  2. NSLog(

@"二维数组:%@"

,my

2

DArray);
  1. //遍历二维数组;

  2. for

    (

int

 i = 

0

; i < [my

2

DArray

 count

]; i++) {
  1. for

    (

int

 j = 

0

; j < firstRow

.count

; j++) {
  1. NSLog(
@"二维数组元素:%@"

,[[my

2

DArray

 objectAtIndex

:i] objectAtIndex :j]);
  1. }

  2. }

  3. }

  4. return

0

;
  1. }

打印结果如下:

image

【使用NSMutableArray初始化二维数组】

使用NSMutableArray初始化的一维数组和二维数组都是可变的,可以进行修改和插入操作;

[objc]

view plain

copy

<embed id="ZeroClipboardMovie_2" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="29" height="15" name="ZeroClipboardMovie_2" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=2&width=29&height=15" wmode="transparent">

print

?

  1. import <Foundation/Foundation.h>

  2. int

    main(

    int

    argc,

    const

    char

    char

    • argv[]) {
  3. @autoreleasepool

    {

  4. NSMutableArray

    *firstRow = [[NSMutableArray

    alloc

    ]

    initWithObjects

    :

    @"11"

    ,

    @"22"

    ,

    @"33"

    ,

    nil

    nil

    ];

  5. NSMutableArray

    *secondRow = [[NSMutableArray

    alloc

    ]

    initWithObjects

    :

    @"44"

    ,

    @"55"

    ,

    @"66"

    ,

    nil

    nil

    ];

  6. NSMutableArray

    *my

    2

    DArray = [[NSMutableArray

    alloc

    ]

    initWithObjects

    :firstRow,secondRow,

    nil

    nil

    ];

  7. //插入一个数据

  8. [[my

2

DArray

 objectAtIndex

:

0

]

 insertObject

:

@"iOS"

 atIndex

:

3

];
  1. NSLog(
@"%@"

,my

2

DArray);
  1. [[my
2

DArray

 objectAtIndex

:

1

]

 insertObject

:

@"OC"

 atIndex

:

0

];
  1. NSLog(
@"%@"

,my

2

DArray);
  1. }

  2. return

0

;
  1. }

打印结果如下:

image

【使用for-in快速遍历二维数组】

[objc]

view plain

copy

<embed id="ZeroClipboardMovie_3" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="29" height="15" name="ZeroClipboardMovie_3" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=3&width=29&height=15" wmode="transparent">

print

?

  1. import <Foundation/Foundation.h>

  2. int

    main(

    int

    argc,

    const

    char

    char

    • argv[]) {
  3. @autoreleasepool

    {

  4. //定义2个一维数组;

  5. NSArray

    *firstRow = [[NSArray

    alloc

    ]

    initWithObjects

    :

    @"1"

    ,

    @"2"

    ,

    @"3"

    ,

    nil

    nil

    ];

  6. NSArray

    *secondRow = [[NSArray

    alloc

    ]

    initWithObjects

    :

    @"4"

    ,

    @"5"

    ,

    @"6"

    ,

    nil

    nil

    ];

  7. //使用一维数组来初始化二维数组;

  8. NSArray

    *my

2

DArray = [[NSArray

 alloc

]

 initWithObjects

:firstRow,secondRow,

 nil

 nil

];
  1. //遍历二维数组;

  2. for

    (

int

 i = 

0

; i < [my

2

DArray

 count

]; i++) {
  1. for

    (

int

 j = 

0

; j < firstRow

.count

; j++) {
  1. NSLog(
@"二维数组元素:%@"

,[[my

2

DArray

 objectAtIndex

:i] objectAtIndex :j]);
  1. }

  2. }

  3. //打印某个维度的一维数组

  4. NSLog(

@"一维数组:%@"

,[my

2

DArray

 objectAtIndex

:

0

]);
  1. //使用for-in快速遍历二维数组中的一维数组

  2. for

    (

NSArray

 *arr in my

2

DArray) {
  1. NSLog(
@"二维数组中的一维数组:%@"

,arr);
  1. }

  2. //使用for-in快速遍历二维数组中的每一个元素

  3. for

    (

NSArray

 *arr in my

2

DArray) {
  1. for

    (

NSString

 *str in arr) {
  1. NSLog(
@"for-in结果:%@"

,str);
  1. }

  2. }

  3. }

  4. return

0

;
  1. }

打印结果:

image

github主页:https://github.com/chenyufeng1991 。欢迎大家访问!

相关文章

网友评论

      本文标题:Objective-C 二维数组详解(转)

      本文链接:https://www.haomeiwen.com/subject/gpncvxtx.html