NSArray ----NSMutableArray

來源:互聯網
上載者:User

標籤:

 

//NSArray

•Foundation中數組(NSArray)是有序的對象集合

•NSArray只能儲存Objective-C的對象,而不能儲存像 int、float這些基礎資料型別 (Elementary Data Type),但是Objective-C對C 相容,所以在Objective-C程式中,仍然可以使用C的 數組來儲存基礎資料型別 (Elementary Data Type)

•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=[NSArrayarrayWithObjects:@"one",@"two",@"no",nil];// 建立數組, nil表示空值,表示參數列表的結束

       NSLog(@"%@",array);

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

       NSLog(@"%@",arr);

       id rr=[array objectAtIndex:1];//用數組索引檢索數組中的元素

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

        

      longint i=[arraycount]; //輸出數組裡的元素個數

        NSLog(@"%ld",i);     

       

      [NSMutableArrayarrayWithCapacity:20];//分配記憶體空間

    

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

      [nsarraddObject:@"no"];//-addObject:向可變數組的末尾添加?一個元素

       NSLog(@"%@",nsarr);

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

   [nsarraddObjectsFromArray:nsarr2];//-addObjectsFromArray:將另外?一個數組的所有元素添加到調用該方法的數組中,將一個數組裡的內容添加到另一個數組中

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

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

    NSLog(@"%@",nsarr2);

   [nsarr2removeObjectAtIndex:0];//-removeObjectAtIndex:移除數組中指定位置上元素

    NSLog(@"%@",nsarr2);

   [nsarr2removeObject:@"b"];//-removeObjectAtIndex:移除數組中指定位置上元素

    NSLog(@"%@",nsarr2);

    

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

    {

      NSLog(@"tem is:%@",tem);//輸出數組裡面的元素

    }

    

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

    

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

    {

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

    }

    

   //}

    [pool drain];

    return 0;

}//

NSArray ----NSMutableArray

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.