• NSArray ----NSMutableArray


    //NSArray

    •Foundation中数组(NSArray)是有序的对象集合

    •NSArray只能存储Objective-C的对象,而不能存储像 intfloat这些基本数据类型,但是Objective-C对C 兼容,所以在Objective-C程序中,仍然可以使用C数组来存储基本数据类型

    •NSArray⼀一旦创建便不可以再对它就进行更改,如果 要进行对数组的增、删、改等操作的话,需要使用 NSArray的子类NSMutableArray来创建对象

     

    //NSArray常用方法

    •+arrayWithObjects:使用⼀一组对象创建⼀一个数 组(注: 1,该方法可以接收可变数目的参数。2, 最后⼀一个值指定为nil,表示参数列表结束)

    •-objectAtIndex:用数组索引检索数组中的元素

    •-count:返回数组元素个数

     

    //NSMutableArray

    •NSMutableArray是NSArray的子类,继承了

    NSArray的所有方法,并添加了新的方法

    •NSMutableArray用来处理可变数组


     

    //+arrayWithCapacity:为可变数组指定初始容量

    •-addObject:向可变数组的末尾添加⼀一个元素

    •-addObjectsFromArray:将另外⼀一个数组的所有元素添加 到调用该方法的数组中

    •-insertObject:atIndex:将⼀一个元素添加到数组指定的位 置上

    •-removeObjectAtIndex:移除数组中指定位置上元素

    •-removeObject:移除数组中指定元素


     

    //快速遍历

    for(类名 *对象名 in 需要遍历的对象)

    {

       //. . .

    }

     

    //

    //  main.m

    //  shiyueshihaoshuzu

    //

    //  Created by iphone on 11-10-10.

    //  Copyright 2011年 __MyCompanyName__. Allrights reserved.

    //


    #import<Foundation/Foundation.h>


    int main (int argc, const char * argv[])

    {


       //@autoreleasepool {

           

          NSAutoreleasePool* pool=[[NSAutoreleasePoolalloc] init];

           // insert codehere...

           NSArray *array=[NSArray arrayWithObjects:@"one",@"two",@"no",nil];//  创建数组, nil表示空值,表示参数列表的结束

            NSLog(@"%@",array);

            NSArray * arr=[NSArray arrayWithObjects:@"hello", nil];

            NSLog(@"%@",arr);

            id rr=[array objectAtIndex:1];//用数组索引检索数组中的元素

            NSLog(@"id%@",rr);

            

           longint i=[array count]; //输出数组里的元素个数

             NSLog(@"%ld",i);     

           

          [NSMutableArrayarrayWithCapacity:20];//分配内存空间

        

          NSMutableArray*nsarr=[NSMutableArrayarrayWithObjects:@"hi",@"hello", nil];

          [nsarr addObject:@"no"];//-addObject:向可变数组的末尾添加⼀一个元素

            NSLog(@"%@",nsarr);

       NSMutableArray *nsarr2=[NSMutableArray arrayWithObjects:@"a",@"b",nil];

       [nsarr addObjectsFromArray:nsarr2];//-addObjectsFromArray:将另外⼀一个数组的所有元素添加到调用该方法的数组中,将一个数组里的内容添加到另一个数组中

       NSLog(@"---------%@",nsarr);

       [nsarr2 insertObject:@"c"atIndex:1];

        NSLog(@"%@",nsarr2);

       [nsarr2 removeObjectAtIndex:0];//-removeObjectAtIndex:移除数组中指定位置上元素

        NSLog(@"%@",nsarr2);

       [nsarr2 removeObject:@"b"];//-removeObjectAtIndex:移除数组中指定位置上元素

        NSLog(@"%@",nsarr2);

        

        for(NSString * tem in array)//遍历

        {

           NSLog(@"tem is:%@",tem);//输出数组里面的元素

        }

        

       NSLog(@"--------------------");

        

        for(inti=0;i<[arraycount];i++)//类似C里面的

        {

           NSLog(@"%@",[array objectAtIndex:i]);

        }

        

       //}

        [pool drain];

        return 0;

    }//

  • 相关阅读:
    实例讲解如何在Delphi中动态创建dxBarManager内容
    SqlServer中创建Oracle连接服务器
    Delphi中的四舍五入函数
    Delphi中的四舍五入函数
    垃圾回收内存管理
    列表生成式
    列表函数&方法
    python切片
    python函数def
    Python 控制流、列表生成式
  • 原文地址:https://www.cnblogs.com/allanliu/p/4246724.html
Copyright © 2020-2023  润新知