Objective-C 二維數組詳解__二維數組

來源:互聯網
上載者:User

     在實際的項目開發中,二維數組也是常常用到的資料結構。OC中的二維數組也是通過一維數組來建立的,今天我們來詳解一下如何在OC中使用二維數組。

【使用NSArray初始化二維數組】

使用NSArray初始化的一維數組和二維數組都是不可變數組。

#import <Foundation/Foundation.h>int main(int argc, const char * argv[]) {  @autoreleasepool {            //定義2個一維數組;    NSArray *firstRow = [[NSArray alloc] initWithObjects:@"1",@"2",@"3", nil];    NSArray *secondRow = [[NSArray alloc] initWithObjects:@"4",@"5",@"6", nil];        //使用一維數組來初始化二維數組;    NSArray *my2DArray = [[NSArray alloc] initWithObjects:firstRow,secondRow, nil];    //輸出二維數組對象;    NSLog(@"二維數組:%@",my2DArray);        //遍曆二維數組;    for (int i = 0; i < [my2DArray count]; i++) {      for (int j = 0; j < firstRow.count; j++) {        NSLog(@"二維數組元素:%@",[[my2DArray objectAtIndex:i] objectAtIndex :j]);      }    }      }  return 0;}

列印結果如下:



【使用NSMutableArray初始化二維數組】

使用NSMutableArray初始化的一維數組和二維數組都是可變的,可以進行修改和插入操作;

#import <Foundation/Foundation.h>int main(int argc, const char * argv[]) {  @autoreleasepool {        NSMutableArray *firstRow = [[NSMutableArray alloc] initWithObjects:@"11",@"22",@"33", nil];    NSMutableArray *secondRow = [[NSMutableArray alloc] initWithObjects:@"44",@"55",@"66", nil];    NSMutableArray *my2DArray = [[NSMutableArray alloc] initWithObjects:firstRow,secondRow, nil];    //插入一個資料    [[my2DArray objectAtIndex:0] insertObject:@"iOS" atIndex:3];    NSLog(@"%@",my2DArray);        [[my2DArray objectAtIndex:1] insertObject:@"OC" atIndex:0];    NSLog(@"%@",my2DArray);          }  return 0;}
列印結果如下:


【使用for-in快速遍曆二維數組】

#import <Foundation/Foundation.h>int main(int argc, const char * argv[]) {  @autoreleasepool {            //定義2個一維數組;    NSArray *firstRow = [[NSArray alloc] initWithObjects:@"1",@"2",@"3", nil];    NSArray *secondRow = [[NSArray alloc] initWithObjects:@"4",@"5",@"6", nil];        //使用一維數組來初始化二維數組;    NSArray *my2DArray = [[NSArray alloc] initWithObjects:firstRow,secondRow, nil];        //遍曆二維數組;    for (int i = 0; i < [my2DArray count]; i++) {      for (int j = 0; j < firstRow.count; j++) {        NSLog(@"二維數組元素:%@",[[my2DArray objectAtIndex:i] objectAtIndex :j]);      }    }        //列印某個維度一維數組    NSLog(@"一維數組:%@",[my2DArray objectAtIndex:0]);        //使用for-in快速遍曆二維數組中的一維數組    for (NSArray *arr in my2DArray) {      NSLog(@"二維數組中的一維數組:%@",arr);    }            //使用for-in快速遍曆二維數組中的每一個元素    for (NSArray *arr in my2DArray) {      for (NSString *str in arr) {        NSLog(@"for-in結果:%@",str);      }    }          }  return 0;}

列印結果:



github首頁:https://github.com/chenyufeng1991  。歡迎大家訪問。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.