Overview:
- The array inside OC is a linear structured data type, within the foundation framework. Divided into mutable arrays (Nsarray) and non-variable groups (Nsmultiarray), where MultiArray inherits from Nsarray, so Nsarray's various methods, Nsmultiarray as can be used.
- The array can be indexed to the corresponding object by subscript.
- The content stored in Nsarray and Nsmultiarray can only be-objects (instances of classes). So the basic data types cannot be stored directly, and can be stored in Nsvalue or NSNumber packages.
Array Data Model:
- The array inside OC is a linear data structure whose data prototype is a linear table. Abstract data types for linear tables (definition + operations) are shown
- There are 8 kinds of operations: about: Adding or deleting the number of changes to find empty
Common Operations for arrays
an array in iOS is an image implementation of this abstract data structure of linear tables, and here's how Apple's handlers implement this linear structure.
1 Initialization: (Initialize operation, set up an array object, and assign value)
1 class Method initialization: (note here Apple's naming specification, class method is generally classwith, the naming specification is actually very important), here are four kinds of cases: an object, multiple objects, using an array, from the file initialization
class method naming specification //initializing one because it is immutable so practicality is not strong nsarray *arr1=[nsarray arraywithobject:@ "Hello"]; Multiple ends with nil as an array nsarray *arr2=[nsarray arraywithobjects:@ "1", @ "2", nil]; The element points to the same position Nsarray *arr3=[nsarray ARRAYWITHARRAY:ARR2]; Initialize an array from a file// Nsarray *arr3=[nsarray arraywithcontentsofurl:<# (Nsurl *) #>];
2 Instance method initialization:
Nsarray *arr4=[[nsarray alloc] initwitharray:arr1];
3 initialization of a mutable array
Added an empty array nsmutablearray *mularray1=[nsmutablearray array]; Initialize, plus capacity nsmutablearray *mularray=[nsmutablearray arraywithcapacity:1];
2 Array Length: (Returns the number of array elements)
Number of elements int count=[arr2 count];
3 Check: Returns the array I object,
which element is accessed nsstring *string1=[arr1 objectatindex:0];
Finds the last object NSLog (@ "%@", [arr2 Lastobject]);
Traverse
Fast traversal for (/* or id*/nsstring *string in Mularray) { }
4: Finds an array based on an object, returns an ordinal if the same object exists, otherwise returns false
Query object position int objindex=[arr2 indexofobject:@ "2"];
Judging if there is this element this is to judge if there is no if (![ ARR2 containsobject:@ "a"]) { NSLog (@ "no"); }
5 Increase: add element at position I
Append the object, return the new array nsarray *arr11=[arr1 arraybyaddingobject:@ "haha"];
immutable groups cannot be modified by arrays, and operations here are mutable.
Add object for (int i=0;i<20;i++) { [mularray addobject:@ "AAA"]; [Mularray addobject:@ "BBB"]; } Add an element based on the index location [Mularray insertobject:@ "BBB" atindex:1];
6 Delete: Remove the specified position element
Delete //delete last [Mularray removelastobject]; Delete the corresponding element [Mularray removeobject:@ "AAA"]; Specify the coordinates to delete [Mularray removeobjectatindex:3]; Delete [Mularray removeobjectsinarray:arr1] According to the array;
7 Change: Modify the specified position element (that is, replace)
Replace [Mularray replaceobjectatindex:0 withobject:@ "1"];
8 null: Empty, return true false
there is no array empty operation in iOS, you can use count==0 to judge
Welcome reprint, Reprint please indicate source: http://blog.csdn.net/zhenggaoxing/article/details/43559869This series of posts directory:OBJECTIVE-C Learning Series Catalog Update not timed
Objective-c Learning Arrays