Objective-c two-dimensional array of __ two-dimensional arrays

Source: Internet
Author: User

In the actual project development, two-dimensional arrays are often used in the data structure. The two-dimensional array in OC is also created by a one-dimensional array, and today we'll explain how to use a two-dimensional array in OC.

"Initialize two-dimensional arrays with Nsarray"

Both one-dimensional and two-dimensional arrays initialized with Nsarray are variable groups.

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
  @autoreleasepool {
    
    
    // Defines 2 one-dimensional arrays;
    nsarray *firstrow = [[Nsarray alloc] initwithobjects:@ "1", @ "2", @ "3", nil];
    Nsarray *secondrow = [[Nsarray alloc] initwithobjects:@ "4", @ "5", @ "6", nil];
    
    Use one-dimensional arrays to initialize two-dimensional arrays;
    nsarray *my2darray = [[Nsarray alloc] initwithobjects:firstrow,secondrow, nil];
    Output two-dimensional array object;
    NSLog (@ "Two-dimensional array:%@", My2darray);
    
    Traversal of two-dimensional arrays; for
    (int i = 0; i < [My2darray count]; i++) {for
      (int j = 0; J < Firstrow.count; J +) {
        NS Log (@ "Two-dimensional array element:%@", [[My2darray objectatindex:i] objectatindex:j]);
  }} return 0;
}

Print the results as follows:



"Initialize two-dimensional arrays with Nsmutablearray"

Both one-dimensional and two-dimensional arrays initialized with Nsmutablearray are mutable and can be modified and inserted;

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
  @autoreleasepool {
    
    Nsmutablearray *firstrow = [[Nsmutablearray alloc] initwithobjects:@ "One", @ "," @ "," nil];
    Nsmutablearray *secondrow = [[Nsmutablearray alloc] initwithobjects:@ "the", @ "", @ "a", nil];
    Nsmutablearray *my2darray = [[Nsmutablearray alloc] initwithobjects:firstrow,secondrow, nil];
    
Insert a data
    [[My2darray objectatindex:0] insertobject:@ "IOS" atindex:3];
    NSLog (@ "%@", My2darray);
    
    [[My2darray objectatindex:1] insertobject:@ "OC" atindex:0];
    NSLog (@ "%@", My2darray);
    
    
  }
  return 0;
}
Print the results as follows:


"Fast traversal of two-dimensional arrays with for-in"

#import <Foundation/Foundation.h> int main (int argc, const char * argv[]) {@autoreleasepool {//definition
    2 one-dimensional array; nsarray *firstrow = [[Nsarray alloc] initwithobjects:@ "1", @ "2", @ "3", nil];
    
    Nsarray *secondrow = [[Nsarray alloc] initwithobjects:@ "4", @ "5", @ "6", nil];
    
    Use one-dimensional arrays to initialize two-dimensional arrays; Nsarray *my2darray = [[Nsarray alloc] initwithobjects:firstrow,secondrow, nil];
        Traversal of two-dimensional arrays; for (int i = 0; i < [My2darray count]; i++) {for (int j = 0; J < Firstrow.count; J +) {
      NSLog (@ "Two-dimensional array element:%@", [[[My2darray objectatindex:i] objectatindex:j]);
    
    }//Print one-dimensional array of a dimension NSLog (@ "one-dimensional array:%@", [My2darray objectatindex:0]);
    Use for-in to quickly traverse a one-dimensional array in a two-dimensional array for (Nsarray *arr in My2darray) {NSLog (a one-dimensional array in a two-dimensional array:%@, arr);
        ///Use for-in to quickly traverse each element of a two-dimensional array for (Nsarray *arr in My2darray) {for (NSString *str in arr) {
      NSLog (@ "for-in results:%@", str);
}} return 0; }

Print results:



GitHub home: https://github.com/chenyufeng1991. You are welcome to visit.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.