1. Common ways to handle arrays
--------------------the//1 of an immutable group. Array creation 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) using class methods to create Nsarray *array2 = [Nsarray arraywithobjects:s1,s2,s3, Nil];
(3) Create an array object while depositing an element into it nsarray *array3 = [Nsarray arraywithobject:s1];
(4) Create an array in which the elements from array1 nsarray *array4 = [Nsarray arraywitharray:array1];
NSLog (@ "Array4 =%@", array4);
2. NSString *STR1 = [Array4 objectatindex:0] by subscript element; 3. Number of array elements nsuinteger count = [Array4 count];
Equivalent to: Array4.count;
4. Determine if 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 Elemnts");} else {NSLog (@ "index =%ld", index),//6. Strings in a linked array (if: array is a string)
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 back of the original path nsarray *array5 = [Array4 arraybyaddingobject:@ "Zhaolia"];
NSLog (@ "array5 =%@", array5);
Take the corresponding subscript element int idx=4;
if (idx <array5.count) {nsstring *s = [Array5 objectatindex:idx];
NSLog (@ "s =%@", s);
}//--------------traversal of array//1. Normal traversal for (int i=0; i<array5.count; i++) {nsstring *str = [Array5 objectatindex:i];
NSLog (@ "%@", str); }//Fast traverse for (NSString *s in Array5) {NSLog (@ "%@", s);}//4.4 after optimization//1. Create a vegetarian 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 is 5, you can change.
(inherited from Nsarray) nsmutablearray *mutablearray = [Nsmutablearray arraywithcapacity:5];
Add an element to the array [Mutablearray addobject:@ "AAA"];
Specifies the subscript insertion element in the array [Mutablearray insertobject:@ "CCC" atindex:0]; NSLog (@ "%@", Mutablearray);
The original position element is then moved//removed from the last element [Mutablearray Removelastobject];
NSLog (after removing the last element:%@, Mutablearray);
Removes the specified element [Mutablearray removeobject:@ "AAA"];
Removes the specified subscript element [Mutablearray removeobjectatindex:0];
Add an array to the array [Mutablearray addobjectsfromarray:array1];
1. Create variable array nsstring *t1 = @ "Zhangsan";
NSString *t2 = @ "Lisi";
NSString *t3 = @ "Wangwu"; Nsmutablearray *marray1 = @[s1,s2,s3];//wrong. This creates an immutable group nsmutablearray *marray1 = [[Nsmutablearray alloc]
INITWITHOBJECTS:S1,S2,S3, nil];
When creating an array, 3 spaces are created to store the elements, storage exceeds the capacity, and the array automatically increases the space nsmutablearray *marray2 = [[Nsmutablearray alloc] initwithcapacity:3];
Nsmutablearray *marray3 = [Nsmutablearray arraywithcapacity:3];
2. Add elements [MArray2 addobject:t1];
[MArray2 Addobject:t2];
[MArray2 ADDOBJECT:T3];
NSLog (@ "marray2=%@", mArray2);
Add the elements in the mArray2 to the MARRAY3//[MArray3 addobjectsfromarray:marray2];
Adding mArray2 as a two-dimensional number [mArray3 addobject:marray2];
NSLog (@ "mArray3 =%@", mArray3);
3. Insert element [mArray2 insertobject:@ "Jack" atindex:0];
NSLog (@ "mArray2 =%@", mArray2); 4. Replace elements [MArray2 replaceobjectatindex:0 withobject:@ "John"];
NSLog (@ "Replace:%@", mArray2);
5. The position of the interchange of two elements [MArray2 exchangeobjectatindex:3 withobjectatindex:0];
NSLog (@ "mArray2 =%@", mArray2);
6. Delete element//6.1 Delete [MArray2 removeobjectatindex:2] according to subscript;
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);
The above is a small set of iOS for you to introduce the Nsarray array of common processing methods, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!