Create an Array object, Nsarray an immutable set of variables
[NSNull null] Empty object
Nsarray *arr = [[Nsarray alloc] initwithobjects:@ "234", @ "543", @ "ASB", Nil];
Nsarray *arr1 = @[@ "1 2 3", @ "234", [NSNull null],@ "789"];
Nsarray *ARR3 = [[Nsarray alloc] initwitharray:arr1];
NSLog (@ "%@", ARR3);
Convert c array to OC Array object
NSString *carr[10] = {@ "123", @ "456", @ "789"};
Nsarray *ARR4 = [[Nsarray alloc] Initwithobjects:carr Count:3];
NSLog (@ "%@", ARR4);
Gets the number of array elements
Nsuinteger count = [ARR4 count]; Nsuinteger unsigned Nsinteger signed
NSLog (@ "Count Is:%ld", count);
Gets the specified subscript element
NSLog (@ "%@", [Arr3 objectatindex:1]);
Gets the first element or the last element
NSLog (@ "%@", [Arr3 Firstobject]);
NSLog (@ "%@", [Arr3 Lastobject]);
Get a subset of ARR2
Nsarray *ARR2 = @[@ "111", @ "222", @ "333", @ "444", @ "555", @ "666"];
Take out successive ranges of elements in arr2 as subsets of content
Nsarray *sub1 = [arr2 subarraywithrange:nsmakerange (1, 3)]; Sub1 for the new subset
NSLog (@ "%@", sub1);
Get any element as a subset
1, list of the necessary elements to do the subscript list
Nsmutableindexset *indexs = [Nsmutableindexset indexset];
1-1 Write a list of the subscript to get the element
for (Nsinteger i = 0; i<[arr2 count]; i++) {
if (i% 2! = 0)
// {
[Indexs addindex:i];
// }
//}
2. Give the list to the array
Nsarray *sub2 = [arr2 objectatindex:indexs];//gets all the elements in the subscript list, returns an element set composite group
NSLog (@ "sub2 =%@", sub2);
Gets the longest string and the smallest string
Quick Enumeration
Nsarray *ARR5 = @[@ "123456789", @ "222", @ "333", @ "errr", @ "444", @ "5", @ "666"];
Nsinteger maxLength = [arr5[0] length];
Nsinteger minLength = [arr5[0] length];
NSString *maxstring = nil;
NSString *minstring = nil;
for (id obj in ARR5) {
if (maxLength <= [obj length]) {
maxLength = [obj length];
maxstring = obj;
}
if (minLength >= [obj length]) {
minLength = [obj length];
minstring = obj;
}
}
NSLog (@ "maxstring =%@, minstring =%@", maxstring,minstring);
////////////////////////////////////////////////////////////////////
========= variable array =========== variable array ============ variable array ===========//
////////////////////////////////////////////////////////////////////
Creating a mutable Array object
Nsmutablearray *array1 = [[Nsmutablearray alloc] initwithobjects:@ "232", @ "AAA", @ "342", @ "987", @ "876", nil];
Nsmutablearray *array2 = [[Nsmutablearray alloc] initwithobjects:@ "Dada", @ "ZAs", @ "4RFC", @ "7uj", nil];
NSLog (@ "array1 =%@ array2 =%@", array1,array2);
inserting elements
Insert Subscript range 0 <= Index <= count
[Array1 insertobject:@ "JXF" atindex:1];
[Array1 insertobject:@ "WG" Atindex:[array1 count]];//This is to insert the "WG" into the last
[Array1 insertobject:@ "GG" atindex:3];//this is to insert "GG" into the position of subscript 3!
NSLog (@ "array1 =%@", array1);
Delete Element
[Array2 removeallobjects];//Delete all
[Array2 removelastobject];//Delete the last
[Array2 removeobject:@ "ZAs"];
NSLog (@ "%@", array2);
[Array2 removeobject:@ "ZAs" Inrange:nsmakerange (1, 2)];
NSLog (@ "%@", array2);
[Array2 removeobjectatindex:2];//Removes the element that specifies the subscript
[Array2 removeobjectidenticalto:@ "ZAs"];
[Array2 removeobjectidenticalto:@ "ZAs" Inrange:nsmakerange (1, 2)];
If you want to delete a discontinuous element, put the element you want to delete in a list before you delete the manifest
For example, to delete an even-numbered
Nsmutableindexset *indexs = [Nsmutableindexset indexset];
for (int i = 0; i < [array2 count]; i++) {
if (i% 2 = = 0) {
[Indexs addindex:i];
}
}
Remove elements from a list
[Array2 Removeobjectsatindexes:indexs];
NSLog (@ "%@", array2);
Modify the contents of an array
[Array2 replaceobjectatindex:1 withobject:@ "NMB"];
NSLog (@ "%@", array2);
Nsarray *arr = @[@ "", @ "111", @ "AAA"];
[Array2 Replaceobjectsatindexes:indexs Withobjects:arr];
NSLog (@ "%@", array2);
Swapping the contents of an array
[Array2 exchangeobjectatindex:0 withobjectatindex:1];
NSLog (@ "%@", array2);
Using OC to sort arrays in C language
for (int i = 0; i<[array2 count]; i++) {
for (Int J =i+1; J<[array2 count];j++) {
if ([array2[i] length]>[array2[j] length]) {
[Array2 exchangeobjectatindex:i withobjectatindex:j];
}
}
}
NSLog (@ "array2 =%@", array2);
OC Learning Nsarray and Nsmutablearray arrays