数组 NSArray NSMutableArray

来源:互联网 发布:企业管理论文选题 知乎 编辑:程序博客网 时间:2024/06/01 17:14

【数组部分】


学习目标:

  • 掌握NSArray类的重点函数使用
  • 掌握NSMutableArray类的重点函数使用
  • 了解NSMutableArrayNSArray的继承关系



1.需要理解的知识

1.1不可变数组

1.1.1OC数组和C数组的区别

1,创建方式不同:c语言中的数组,int a[10]={1,2,3,4,5,6,7,8,9,10};

  oc 里边的数组,NSArray *array = [[NSArray  alloc]  initWithObjects:@"one"

,@"two",@"three",nil];

2,   C语言中的数组存的是一般数据类型,但是OC数组中存的都是对象

1.1.2创建数组对象


Objective-c 2.0新特性

//这种方式不是很常用,但是要了解,

//需要注意的时,这中数组创建方式结尾处没有NIL

NSArray * array3 =@[@"One One",@"Two", @"Three",@"Four", dog];


1.1.3数组元素

数组元素是任意类型的对象地址


1.1.4遍历数组 

for循环遍历

NSArray *array = [[NSArrayalloc] initWithObjects:@"a",@"b",@"c",@"d",nil];

       //数组遍历

        //返回数组中的元素个数

       NSUInteger i = [arraycount];

       NSLog(@"%lu",i);

       

        //返回指定的下标元素

        //元素是什么类型,就要用什么类型去接收objectAtIndex:的返回值

        //但是数组中有可能存在着不同类型的对象,所以,我们用(id)类型来接收

       // NSString *str = [array objectAtIndex:i];

       for (int j=0 ; j<i; j++) {

          id str2 = [arrayobjectAtIndex:j];

           NSLog(@"%@",str2);

       }


枚举法遍历(了解就行)

  NSArray *array = [[NSArrayalloc] initWithObjects:@"one",@"two",@"three",nil];

        //通过数组的实例方法:objectEnumerator给枚举变量赋值

       NSEnumerator *enumerator  = [arrayobjectEnumerator];

        

       id obj;

//枚举指针位置在下标零的位置之前

  //因为[enumerator nextObject]调用后打印的第一个元素就是下标为零的元素。

       while (obj = [enumeratornextObject]) {

           NSLog(@"%@",obj);

       }


快速枚举法 for in结构

NSArray *array = [[NSArrayalloc] initWithObjects:@"one",@"two",@"three",@"four",nil];

        

       //快速枚举遍历数组

        //快速枚举的特点,即便没有  i++的过程,但是也在做循环功能

        //快速枚举的语法:id obj in 数组名

        //id obj 是定义指向任何类型对象的指针

// id   后边不用加 *  加了就是错误的

       for (id objin array) {

           NSLog(@"%@",obj);

       }

1.2可变数组

<1>对象内容可以改变

<2>可变数组与不可变数组的关系

NSMutableArray 继承于 NSArray

<3>对可变数组对象进行增删改操作

<4>可变数组的排序

1.使用冒泡/选择法两层for循环对可变数组排序

2.

使用方法:- (void)sortUsingSelector(SEL)comparator;

:这个排序方法已经实现,需要我们提供一个排序准则。

我们需要把一个比较方法转化为SEL选择器类型传入上述排序方法

这个比较方法需要写在数组元素所在类中


1.3数字对象

<1>如何把基本数据类型(int char long NSInteger BOOL)存放到OC数组或者字典对象中

转化为数字对象NSNumber

<2>如何把基本数据类型(int char long NSInteger BOOL)包装成数组对象

1.4 - description; 

描述对象信息 :将对象的属性信息转化为字符串

//当打印本类对象时,将自动调用description方法,

//返回值一般为NSString *  但是也可以为其他类型,

//它是一个特殊的实例方法,不用在.H文件中声明,直接在。m中实现

-(NSString *)description;

//返回一个_age的值

-(NSString *)description

{

    NSString *str = [[NSStringalloc] initWithFormat:@"%i",_age];

   return str;

}


eg:

//创建一个学生对象(自己创建一个学生类)

       Student *stu1 = [[Studentalloc] init];

       [stu1 setAge:20];

        //创建一个字符串对象

       NSString *str =@"abcd";

       //创建一个可变数组

        NSMutableArray *muarray =[[NSMutableArrayalloc] initWithObjects:@"one",@"two",@"three",nil];

       //添加对象

       [muarray addObject:str];

       [muarray addObject:stu1];

       NSLog(@"%@",muarray);

       //如果没有重写description方法,以上的

        

       for (id objin muarray) {

           NSLog(@"%@",obj);

        }


1.5 字符串分割

2.需要记住的知识

2.1 NSArray

2.1.1方法

<1>常见创建方法

- (id)initWithObjects:(id)firstObj, ... ;(记住)

//OC中使用数组,必须将数组实例化成对象

NSArray *array = [[NSArrayalloc] initWithObjects:@"one",@"two",@"three",@"four",nil];

- (id)initWithArray:(NSArray *)array;

//因为内存管理技术,导致initwithArray的数组,与原数组的地址不同  

//OC中使用数组,必须将数组实例化成对象

        NSArray *array = [[NSArrayalloc] initWithObjects:@"one",@"two",@"three",@"four",nil];

       NSLog(@"%@",array);

        //以一个数组对象创建另外一个新的数组

       NSArray *array2 = [[NSArrayalloc] initWithArray:array];

       NSLog(@"%@",array2);

       NSLog(@"%p%p",array,array2);

+ (id)arrayWithObjects:(id)firstObj, ... ;

//用类方法创建,和initWithObjects:创建原理一样

        NSArray *array = [NSArrayarrayWithObjects:@"a",@"b" ,nil];

+ (id)arrayWithArray:(NSArray *)array;

  NSArray *array = [NSArrayarrayWithObjects:@"a",@"b" ,nil];

       //以一个数组创建另外一个数组对象

       NSArray *array2 = [NSArrayarrayWithArray:array];

       NSLog(@"%@",array2);

<2>获取数组元素个数

- (NSUInteger)count;

  NSArray *array = [[NSArrayalloc] initWithObjects:@"a",@"b",@"c",@"d",nil];

       //数组遍历

        //返回数组中的元素个数

       NSUInteger i = [arraycount];

       NSLog(@"%lu",i);

<3>通过索引获取相应的元素

- (id)objectAtIndex:(NSUInteger)index;

NSArray *array = [[NSArrayalloc] initWithObjects:@"a",@"b",@"c",@"d",nil];

        

        //元素是什么类型,就要用什么类型去接收objectAtIndex:的返回值

        

           

NSString *str = [array objectAtIndex:i];

       

           NSLog(@"%@",str);

        

<4>通过对象地址获取在数组中的索引

- (NSUInteger)indexOfObject:(id)anObject;

  NSArray *array = [[NSArrayalloc] initWithObjects:@"one",@"two",@"three",@"four",nil];

        //indexOfObject: 翻译:元素所对应的下标

        //indexOfObject       objectOfIndex:作用相反,

       NSUInteger i =[arrayindexOfObject:@"two"];

       NSLog(@"%lu",i);

<5>判断数组中数组包含元素anObject

- (BOOL)containsObject:(id)anObject;

NSArray *array = [[NSArrayalloc] initWithObjects:@"one",@"two",@"three",nil];

        //containsObject:翻译:是否包含某个元素,返回BOOL类型

       BOOL b = [arraycontainsObject:@"one"];

       if (b) {

           NSLog(@"YES");

       }

       else

       {

           NSLog(@"NO");

       }

<6>获取数组的最后一个元素

- (id)lastObject;

NSArray *array = [[NSArrayalloc] initWithObjects:@"one",@"two",@"three",nil];

//取数组中的最后一个元素。

       NSLog(@"%@",[arraylastObject]);

<7>把数组元素内容按照字符串separator进行拼接

- (NSString *)componentsJoinedByString:(NSString *)separator;(记住)

NSArray *array = [[NSArrayalloc] initWithObjects:@"one",@"two",@"three",nil];

 //把数组中的元素按照后边字符串为间隔拼接成字符串,并返回字符串的地址,

            NSString *str = [array componentsJoinedByString:@" "];

       NSLog(@"%@",str);

        //结果是:one two three

        NSString *str2 = [arraycomponentsJoinedByString:@"@"];

       NSLog(@"%@",str2);

        //结果是:one@two@three

<8>把字符串内容按照字符串separator进行分割,并存到数组中

-(NSArray *)componentsSeparatedByString:(NSString *)separator;

//按照componentsSeparatedByString:后边的字符把字符串分割,并存到一个数组里,返回数组的地址

        NSArray *array2 = [str2componentsSeparatedByString:@"@"];

       for (id objin array2) {

           NSLog(@"%@",obj);

       }

    •         <9>//按照字符串整体分割  
  • - (NSArray *)componentsSeparatedByString:(NSString *)separator;
    •      //按照componentsSeparatedByString:后边的字符把字符串分割,并存到一个数组里,返回数组的地址
  •       NSArray *array2 = [str2componentsSeparatedByString:@"@"];
  •         for (id objin array2) {
  •             NSLog(@"%@",obj);
  •         }
    •         <10>   //按照字符集合分割
  • - (NSArray *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator
    •         NSString *str4 = @"ab,ab#ab";
  •        //componentsSeparatedByCharactersInSet:按照指定字符集将字符串分割成单个字符串对象存入数组,并返回数组地址
  •        //NSCharacterSet characterSetWithCharactersInString::@",#"
  •         //用指定的字符串创建一个字符集
  •         NSArray *arrya3 = [str4componentsSeparatedByCharactersInSet:[NSCharacterSetcharacterSetWithCharactersInString:@",#"]];
  •                            
  •         NSLog(@"%@",arrya3);


2.2NSMutableArray

2.2.1方法

<1>增加数组元素

//追加元素

     - (void)addObject:(id)anObject;

 //创建一个学生对象(自己创建一个学生类)

       Student *stu1 = [[Studentalloc] init];

       [stu1 setAge:20];

        //创建一个字符串对象

       NSString *str =@"abcd";

       //创建一个可变数组

        NSMutableArray *muarray =[[NSMutableArrayalloc] initWithObjects:@"one",@"two",@"three",nil];

       //添加对象

       [muarray addObject:str];

       [muarray addObject:stu1];

       NSLog(@"%@",muarray);


//指定索引插入元素

  - (void)insertObject:(id)anObject atIndex:(NSUInteger)index;

NSMutableArray *array = [[NSMutableArrayalloc] init];

        

        [arrayaddObject:@"one"];

        [arrayaddObject:@"two"];

        [arrayaddObject:@"three"];

       NSLog(@"%@",array);

        //  insertObject: 插入的对象 atIndex:插入的下标

        // 在指定下标插入对象

        [arrayinsertObject:@"four"atIndex:3];

       NSLog(@"%@",array);

    //追加一个数组

  - (void)addObjectsFromArray:(NSArray *)otherArray;

 NSArray *array = [[NSArrayalloc] initWithObjects:@"i",@"love",@"so",@"much",nil];

        NSMutableArray *muarray = [[NSMutableArrayalloc] initWithObjects:@"hello",nil];

        //把另外数组(可变和不可变都行)中的元素追加到当前数组中

        [muarrayaddObjectsFromArray:array];

       NSLog(@"%@",muarray);

  <2>删除

//删除最后一个元素

  - (void)removeLastObject;

NSMutableArray *muarray = [[NSMutableArrayalloc] initWithObjects:@"hello",nil];

//删除可变中的最后一个元素

       [muarray removeLastObject];

       NSLog(@"%@",muarray);

//删除指定索引的元素

  - (void)removeObjectAtIndex:(NSUInteger)index;

NSMutableArray *muarray = [[NSMutableArrayalloc]initWithObjects:

@"hello",@"world",@"i",@"love",@"so",@"much",nil];

//删除指定下标的元素

       [muarray removeObjectAtIndex:1];

       NSLog(@"%@",muarray);

//删除所有元素

  - (void)removeAllObjects;


NSMutableArray *muarray = [[NSMutableArrayalloc]initWithObjects:

@"hello",@"world",@"i",@"love",@"so",@"much",nil];

//删除数组中所有的元素

       [muarray removeAllObjects];

       NSLog(@"%@",muarray);

//在一定范围删除指定的元素

  - (void)removeObject:(id)anObject inRange:(NSRange)range;

NSMutableArray *muarray = [[NSMutableArrayalloc]

initWithObjects:@"hello",@"world",@"i",@"love",@"so",@"much",nil];

//删除数组中指定范围内的指定元素

[muarray removeObject:@"so"inRange:NSMakeRange(2, 3)];

         NSLog(@"%@",muarray);

//删除指定的元素

  - (void)removeObject:(id)anObject;

 

NSMutableArray *muarray = [[NSMutableArrayalloc]

    initWithObjects:@"hello",@"world",@"i",@"love",@"so",@"much",nil];

 //删除指定的元素

       [muarray removeObject:@"world"];

       NSLog(@"%@",muarray);

//根据一个数组删除指定的元素

  - (void)removeObjectsInArray:(NSArray *)otherArray;

  NSArray *array = [[NSArrayalloc] initWithObjects:@"hello",@"world",nil];

        //删除可变数组中在后边数组中也存在对象,即删除两数组中的交集。

       [muarray removeObjectsInArray:array];

       NSLog(@"%@",muarray);

  <3>修改数组

   - (void)setArray:(NSArray *)otherArray;

NSMutableArray *muarray = [[NSMutableArrayalloc

initWithObjects:@"hello",@"world",@"i",@"love",@"so",@"much",nil];

//修改数组,将前边数组的元素清空,把后边数组中的元素赋值进去

       [muarray setArray:array];

       NSLog(@"%@",muarray);

  <4>替换指定索引的元素

  - (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;

//将指定坐标的对象替换成指定的对象

        [muarrayreplaceObjectAtIndex:1 withObject:@"i am exchange"];

       NSLog(@"%@",muarray); 

<5>交换数组元素

  - (void)exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2;

NSMutableArray *mutArray = [[NSMutableArrayalloc] initWithObjects:@"a",@"b",@"c",@"d",@"e",nil];

 //交换两个指定下标下的对象

        [mutArrayexchangeObjectAtIndex:1 withObjectAtIndex:2];

    

       NSLog(@"%@",mutArray);


  <6>排序:根据排序准则排序

  - (void)sortUsingSelector:(SEL)comparator;

- (NSComparisonResult)compare:(Student *)stu

{

    //如果传的参数的大于本身参数,那就返回升序,并按照升序进行排序

   if ([stu age]>_age) {

        returnNSOrderedAscending;

   }

   else if([stu age] <_age)

   {

        return NSOrderedDescending;

   }

   else

   {

       return  NSOrderedSame;

   }



//在当前文件所有加载的类中,去查找@selector需要的方法

       SEL sel =@selector(compare:);

       //自动排序

       //配合比较方法

        //sortedArrayUsingSelector: 它返回一个NSArray * 的对象,必须用一个相应的对象接一下,

        //被排序的数组不会改变

       NSArray *arr = [stuArraysortedArrayUsingSelector:sel];

        

       NSLog(@"%@",arr);

0 0