IOS, ios9
1. Common Array Processing Methods
// -------------------- Immutable Array
// 1. Create an array
NSString * s1 = @ "zhangsan ";
NSString * s2 = @ "lisi ";
NSString * s3 = @ "wangwu ";
// (1)
NSArray * array1 = [[NSArray alloc] initWithObjects: s1, s2, s3, nil];
NSLog (@ "% @", array1); // equivalent to array1.descripton
// (2) create using the Class Method
NSArray * array2 = [NSArray arrayWithObjects: s1, s2, s3, nil];
// (3) create an array object and store an element in it
NSArray * array3 = [NSArray arrayWithObject: s1];
// (4) Create an array with elements from array1
NSArray * array4 = [NSArray arrayWithArray: array1];
NSLog (@ "array4 = % @", array4 );
// 2. Obtain elements by subscript
NSString * str1 = [array4 objectAtIndex: 0];
// 3. Number of array elements
NSUInteger count = [array4 count]; // equivalent to: array4.count;
// 4. Determine whether the array contains an element
BOOL isContains = [array4 containsObject: @ "zhangsan"];
NSLog (@ "isContains: % d", isContains );
// 5. Find the subscript position of an object in the array
NSUInteger index = [array4 indexOfObject: @ "wangwu"];
If (index = NSNotFound ){
NSLog (@ "Not find elemets ");
} Else {
NSLog (@ "index = % ld", index );
}
// 6. link the string in the array (prerequisite: All strings in the array)
NSString * joinString = [array4 componentsJoinedByString: @ ","];
NSLog (@ "joinString = % @", joinString );
// 7. Access the last element of the array
NSString * lastObj = [array4 lastObject]; // array4.lastObject
NSLog (@ "lsatObj = % @", lastObj );
// 8. Add an element to the original array.
NSArray * array5 = [array4 arrayByAddingObject: @ "zhaolia"];
NSLog (@ "array5 =%@", array5 );
// Obtain the corresponding subscript Element
Int idx = 4;
If (idx <array5.count ){
NSString * s = [array5 objectAtIndex: idx];
NSLog (@ "s = % @", s );
}
// -------------- Array Traversal
// 1. Normal Traversal
For (int I = 0; I <array5.count; I ++ ){
NSString * str = [array5 objectAtIndex: I];
NSLog (@ "% @", str );
}
// Fast Traversal
For (NSString * s in array5 ){
NSLog (@ "% @", s );
}
// Optimization after 4.4
// 1. Create a group
NSArray * array7 = @ [s1, s2, s3]; // equivalent to: NSArray * array7 = [NSArray arrayWithObjects: s1, s2, s3, nil];
NSLog (@ "array7 =%@", array7 );
NSString * str = array7 [0];
NSLog (@ "array [7] = % @", str );
// ------------------ Variable array
// Initialization. Set the number of elements to 5, which can be changed. (Inherited from NSArray)
NSMutableArray * mutableArray = [NSMutableArray arrayWithCapacity: 5];
// Add an element to the array
[MutableArray addObject: @ "aaa"];
// Insert an element to the specified subscript in the array
[MutableArray insertObject: @ "ccc" atIndex: 0];
NSLog (@ "% @", mutableArray); // move back the original position Element
// Remove the last element
[MutableArray removeLastObject];
NSLog (@ "after the last element is removed: % @", mutableArray );
// Remove the specified Element
[MutableArray removeObject: @ "aaa"];
// Remove the specified subscript Element
[MutableArray removeObjectAtIndex: 0];
// Add an array to the array
[MutableArray addObjectsFromArray: array1];
// 1. Create a variable array
NSString * t1 = @ "zhangsan ";
NSString * t2 = @ "lisi ";
NSString * t3 = @ "wangwu ";
// NSMutableArray * mArray1 = @ [s1, s2, s3]; // wrong. an unchangeable array is created here.
NSMutableArray * mArray1 = [[NSMutableArray alloc] initWithObjects: s1, s2, s3, nil];
// When creating an array, three spaces are opened for storing elements. When the storage exceeds the capacity, the array automatically increases the space.
NSMutableArray * mArray2 = [[NSMutableArray alloc] initWithCapacity: 3];
NSMutableArray * mArray3 = [NSMutableArray arrayWithCapacity: 3];
// 2. Add an element
[MArray2 addObject: t1];
[MArray2 addObject: t2];
[MArray2 addObject: t3];
NSLog (@ "mArray2 = % @", mArray2 );
// Add the elements in mArray2 to mArray3
// [MArray3 addObjectsFromArray: mArray2];
// Add mArray2 as a two-dimensional number
[MArray3 addObject: mArray2];
NSLog (@ "mArray3 = % @", mArray3 );
// 3. insert an element
[MArray2 insertObject: @ "Jack" atIndex: 0];
NSLog (@ "mArray2 = % @", mArray2 );
// 4. Replace Element
[MArray2 replaceObjectAtIndex: 0 withObject: @ "John"];
NSLog (@ "Replace: % @", mArray2 );
// 5. Swap the positions of two elements
[MArray2 exchangeObjectAtIndex: 3 withObjectAtIndex: 0];
NSLog (@ "mArray2 = % @", mArray2 );
// 6. delete an element
// 6.1 Delete according to subscript
[MArray2 removeObjectAtIndex: 2];
NSLog (@ "mArray2 = % @", mArray2 );
// 6.2 delete the last element
[MArray2 removeLastObject];
NSLog (@ "mArray2 = % @", mArray2 );
// 6.3 Delete the specified object.
// [MArray2 removeObject: @ "zhangsan"];
// 6.4 delete all elements
[MArray2 removeAllObjects];
NSLog (@ "mArray2 = % @", mArray2 );