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.