美文网首页
Object c数组的操作

Object c数组的操作

作者: 奔跑的小小鱼 | 来源:发表于2017-04-10 22:24 被阅读14次

本节学习内容:

1.为数组添加对象

2.数组的提取

3.字符串分割

4.数组元素拼接

5.数组遍历


【main.m】

#import<Foundation/Foundation.h>

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

@autorelesasepool{

【1.为数组添加对象】

//数组的添加

NSArray *arrray1=[NSArray arrayWithObjects:@"one",@"two',nil];

NSLog(@"array1=@",array1);

打印结果:array1=(one,two)

//给数组添加一个无素,需求接受该方法的返回值,并不是直接修改原数组对象而是创建一个新的临时数组

array1=[array1 arrayByAddingObject:"three"];

NSLog(@"array1=@",array1);

打印结果:array1=(one,two,three)

//给数组添加多个元素

array1=[array1 arrayByAddingObjectsFromArray:@[@"four",@"five",@"six"]];

NSLog(@"array1=@",array1);

打印结果:array1=(one,two,three,four,five,six)


【2.数组的提取】

//提取数组中指定范围的元素,上标默认从0开始

NSArray *subArray1=[arrary1 subarrayWithRang:NSMakRange(2,3)];

NSLog(@"subArray1=@",subArray1);

打印结果:array1=(three,four,five)

//创建一个可变的下标集合对像

NSMutableIndexSet *indexSet=[NSMutableIndexSet indexSetWithIndex:1];

//创建一个可变集合对象添加元素

[indexSet addIndex:3];

[indexSet addIndex:4];

[NSArray (subArray2=[array1 objectsAtIndexes:];

NSLog(@"subArray2=@",subArray2);

打印结果:subArray2=(two,four,five)


【3.字符串分割】

NSString *str="I am a   good  boy";

//返回值是一个数组对象,以字符串整体进行分隔

NSArray *array2=[str conponentsSeparateByString:@" "];

NSLog(@"array2=@",array2);

打印结果:subArray2=(I,am,a,"","",good,"",boy)

NSString *str1=@"I :am :good :boy";

NSArray *array3=[str componentsSeparatedByString:@" :"];

NSLog(@"array3=@",array3);

打印结果:subArray3=("I am a good boy")

//以字符集的方式进入分割,以字符集合中的每一个字符作为分割符

NSArray *array4=[str1 componentsSeparatedByCharactersInSet:NSCharacteSet characterSetwithCharactersInString:@":"]];

NSLog(@"array4=@",array3);

打印结果:subArray4=("I ,"",am,"" ,good ,"",boy")

【4.数组元素拼接】

NSArray *array5=@[@"one',@"two",@"five",[NSNumber numberWithInt:123]];

NSString *str3=[array5 componentsJoindyByString:@" "];

//@" "表示连接方式当前是空格

NSLog(@"str3=@",str3);

打印结果:str3=one two five 123

【5.数组遍历】

1.通过数组元素的下标遍历数组

NSArray *sortArray=@[@"hello",@"welcome",@"china",@"world',@"baidu'];

//求出数组元素个数

SNInteger cnt=[sortArray count];

for(NSInteger i=0; i<cnt;i++){

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

打印结果: hello  welcome  china  world baidu

2.通过枚举器法

//创建一个倒序的枚举器,倒序遍历数组,返回值NSEnumerator对像

NSEnumerator *reverseEnum=[sorArray reverseObjectEnumerator];

id objec=nil;

while(obj=[reverseEnum nextObject]){

NSLog(@"obj=%@,obj");

打印结果:obj=baidu  world  china welcome hello

//创建一个正序的枚举器

NSEnumerator *enmuerator=[sortArray objectEnumerator];

id obj2=nil;

while (obj2=[enumerator nextObjext]){

NSLog(@"obj2=%@",obj2);

}

打印结果:obj2= hello welcome china world baidu

3.快速枚举法

//oc语法新引入的循环结构,obj3是否在sortArray中继续循环,能从soortArray中取出元素,判断是否在数组中,在继续循环,不在侧退出循环

for(id obj3 in sortArray){

NSLog(@"obj3=%@",obj3);

//打印结果:obj3=hello welcome china world baidu

}

return 0;

}

@end

相关文章

  • Object c数组的操作

    本节学习内容: 1.为数组添加对象 2.数组的提取 3.字符串分割 4.数组元素拼接 5.数组遍历 【main.m...

  • Object c数组的创建及操作

    本节学习内容: 1.数组的概念 2.数组对象的生成和初始化 3.访问数组中的元素 4.数组的比较 5.数组的排序 ...

  • Swift中使用optional

    为什么需要optional Object-C 里的 nil 是无类型的指针 Object-C 里面的数组、字典、集...

  • JS对象 & JSON & JS数组操作

    JS对象 & JSON & JS数组操作 JSON 格式(JavaScript Object Notation 的...

  • JavaScript Array与String简单使用

    JS对象:数组 对比Object-C中的数组,JavaScript数组有一些不一样的特点JavaScript数组:...

  • JS判断数组

    说明 JS中要区分数组和非数组对象有时候非常的困难,typeof操作符在对数组操作是返回的是 'Object'(除...

  • 前端的基本操作

    数组以及操作方法 在JavaScript中数组就是类似与Python中的列表 其实数组也是一种object 数组的...

  • 2016.10.23

    处理昨天的笔记 数组的操作数组的拷贝System.arraycopy(object src, int srcpos...

  • 对象属性的遍历

    关于对象属性的遍历,有两个方法: Object.keys()与数组操作结合 for/in循环 Object.key...

  • ArrayList 源码浅析

    简介 ArrayList就是动态数组,数组元素的类型为Object类型,对ArrayList的所有操作底层都是基于...

网友评论

      本文标题:Object c数组的操作

      本文链接:https://www.haomeiwen.com/subject/slmeattx.html