OC 7-day container type lesson7

Source: Internet
Author: User

Common nsarray Methods

1. Create an array object and use the initialization method or constructor.

<Span style = "font-size: 18px;"> nsarray * arr1 = [[nsarray alloc] initwithobjects: @ "Wang Chen", @ "Liu Guowei ", @ "Zheng haikun", nil]; nsarray * arr2 = [nsarray arraywithobjects: @ "Li Zhi", @ "Wei David", @ "Liu tianwei", nil]; nsarray * arr3 = [nsarray arraywitharray: arr1]; nslog (@ "% @ \ n", arr1, arr2, arr3); </span>

Running result:

19:29:06. 750 oc07 _ container class [5676: 303] (

"\ U738b \ u6668 ",

"\ U5218 \ u56fd \ u4f1f ",

"\ U90d1 \ u6d77 \ u5764"

)

(

"\ U674e \ u667a ",

"\ U9b4f \ u5927 \ u536b ",

"\ U5218 \ u5929 \ u4f1f"

)

(

"\ U738b \ u6668 ",

"\ U5218 \ u56fd \ u4f1f ",

"\ U90d1 \ u6d77 \ u5764"

)


2. Obtain the number of elements.

<span style="font-size:18px;"><span style="white-space:pre"></span>for (int i = 0; i < [arr1 count]; i++) {</span>

3. Obtain the object based on the index value.

<span style="font-size:18px;">        NSLog(@"%@",[arr1 objectAtIndex:i]);        NSLog(@"%@", arr1[i]);        }</span>
Running result:

19:29:06. 752 oc07 _ container class [5676: 303] Wang Chen

19:29:06. 752 oc07 _ container class [5676: 303] Wang Chen

19:29:06. 752 oc07 _ container class [5676: 303] Liu Guowei

19:29:06. 753 oc07 _ container class [5676: 303] Liu Guowei

19:29:06. 753 oc07 _ container class [5676: 303] Zheng haikun

19:29:06. 754 oc07 _ container class [5676: 303] Zheng haikun

4. Obtain the index value of an object in the array.

<Span style = "font-size: 18px;"> <span style = "white-space: pre"> </span> nslog (@ "% lu ", [arr1 indexofobject: @ "Wang Chen"]); </span>
Running result:

19:29:06. 754 oc07 _ container class [5676: 303] 0



Common nsmutablearray Methods

1. Create an array object.

<span style="font-size:18px;"><span style="white-space:pre"></span>NSMutableArray * mutArry1 = [NSMutableArray arrayWithArray:arr1];</span>
2. Add and insert elements.
<Span style = "font-size: 18px;"> [mutarry1 addobject: @ "Chen jiahong"]; // Add [mutarry1 addobjectsfromarray: arr2]; nsstring * temp = @ "woca"; [mutarry1 insertobject: temp atindex: 4]; // insert for (INT I = 0; I <[mutarry1 count]; I ++) {nslog (@ "% @", mutarry1 [I]) ;}</span>
Running result:

20:23:31. 004 oc07 _ container class [6755: 303] Wang Chen

20:23:31. 004 oc07 _ container class [6755: 303] Liu Guowei

20:23:31. 005 oc07 _ container class [6755: 303] Zheng haikun

20:23:31. 005 oc07 _ container class [6755: 303] Chen jiahong

20:23:31. 005 oc07 _ container class [6755: 303] woca

20:23:31. 006 oc07 _ container class [6755: 303] Li Zhi

20:23:31. 006 oc07 _ container class [6755: 303] Wei David

20:23:31. 007 oc07 _ container class [6755: 303] Liu tianwei

Nested traversal of Arrays:

<Span style = "font-size: 18px;"> nsmutablearray * class19 = [nsmutablearray arraywithobjects: arr1, arr2, arr3, nil]; // method 1 for (INT I = 0; I <[class19 count]; I ++) {for (Int J = 9; j <[class19 [I] Count]; j ++) {nslog (@ "% @", class19 [I] [J]) ;}// method 2 for (INT I = 0; I <[class19 count]; I ++) {for (Int J = 0; j <[class19 objectatindex: I] Count]; j ++) {nslog (@ "% @", [[class19 objectatindex: I] objectatindex: J]) ;}// method 3 for (INT I = 0; I <[class19 count]; I ++) {nsarray * arr = [class19 objectatindex: I]; for (Int J = 0; j <[arr count]; j ++) {nslog (@ "% @", arr [J]) ;}</span>

3. Delete and replace elements.

<Span style = "font-size: 18px;"> [mutarry1 removelastobject]; [mutarry1 removeobject: @ "Liu tianwei"]; // Replace [mutarry1 replaceobjectatindex: 5 withobject: @ ""]; for (INT I = 0; I <[mutarry1 count]; I ++) {nslog (@ "% @", mutarry1 [I]) ;}</span>


Running result:

20:28:01. 038 oc07 _ container class [6782: 303] Wang Chen

20:28:01. 038 oc07 _ container class [6782: 303] Liu Guowei

20:28:01. 039 oc07 _ container class [6782: 303] Zheng haikun

20:28:01. 039 oc07 _ container class [6782: 303] Chen jiahong

20:28:01. 039 oc07 _ container class [6782: 303] woca

20:28:01. 040 oc07 _ container class [6782: 303] Prince Jie

20:28:01. 040 oc07 _ container class [6782: 303] Wei David

4. Swap two elements at the specified position.

<span style="font-size:18px;">        [mutArry1 exchangeObjectAtIndex:4 withObjectAtIndex:5];        for (int i = 0; i < [mutArry1 count]; i++) {            NSLog(@"%@",mutArry1[i]);        }</span>

About sorting:

<span style="font-size:18px;">NSMutableArray *aryNum = [NSMutableArray arrayWithObjects:@"3", @"7", @"9", @"1", @"5", nil];</span>

Bubble Method:

<span style="font-size:18px;">        int flag = 1;        for (int i = 0; i < [aryNum count ] - 1 && 1 == flag; i++) {            flag = 0;            for (int j = 0; j < [aryNum count] - i - 1; j++) {                if ([aryNum[j] compare: aryNum[j + 1]] == 1) {                    [aryNum exchangeObjectAtIndex:j withObjectAtIndex:j + 1];                    flag = 1;                }            }        }        for (int i = 0; i < [aryNum count]; i++) {            NSLog(@"%@",aryNum[i]);        }</span>
<Span style = "font-size: 18px;"> 1. variable array sorting method [arynum sortusingselector: @ selector (compare :)]; 2. the sorting method of an unchangeable array is arynum = (nsarray *) [arynum sortedarrayusingselector: @ selector (compare :)]; </span>



Common nsnumber Methods

<Span style = "font-size: 18px;"> // 1. create a numeric object of each type // (1) integer nsnumber * n1 = [nsnumber numberwithint: 43]; // (2) floating point (single precision) nsnumber * n2 = [nsnumber numberwithfloat: 3.14]; // (3) floating point (Double Precision) nsnumber * N3 = [nsnumber numberwithdouble: 65.1131313123123123123]; // (4) Long Integer nsnumber * N4 = [nsnumber numberwithlong: 987654321]; // (5) converted nsnumber * N5 = [nsnumber numberwithchar: 'a']; </span>


Common nsmutablearray Methods

<span style="font-size:18px;">        NSMutableArray *arrNum = [NSMutableArray arrayWithObjects:n1, n2, n3, n4, n5, nil];        NSLog(@"%@",arrNum);</span>
Running result:

20:28:01. 044 oc07 _ container class [6782: 303] (

43,

"3.14 ",

"65.11313131231232 ",

987654321,

65

)

Sort arrnum:

<span style="font-size:18px;">        [arrNum sortUsingSelector:@selector(compare:)];        NSLog(@"%@",arrNum);</span>
Running result:

20:28:01. 045 oc07 _ container class [6782: 303] (

"3.14 ",

43,

65,

"65.11313131231232 ",

987654321

)


Traverse arrnum

        for (int i = 0; i < [arrNum count]; i++) {            double d = [arrNum[i] doubleValue];            NSLog(@"%.9f",d);        }


Running result:

20:28:01. 045 oc07 _ container class [6782: 303] 3.140000105

20:28:01. 045 oc07 _ container class [6782: 303] 43.000000000

20:28:01. 046 oc07 _ container class [6782: 303] 65.000000000

20:28:01. 046 oc07 _ container class [6782: 303] 65.113131312

20:28:01. 047 oc07 _ container class [6782: 303] 987654321.000000000







OC 7-day container type lesson7

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.