Object -C 数组 -- 笔记
2015-08-23 18:05
429 查看
//
// main.m
// Array
//
// Created by facial on 23/8/15.
// Copyright (c) 2015 facial_huo. All rights reserved.
//
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
NSLog(@"Hello, World!");
//初始化一个不可变数组
NSArray *arry = [NSArray arrayWithObjects:@"one one", @"two", @"three", nil ];
NSLog(@"%@", arry);
//打印数组方法
//枚举器法
NSEnumerator * Enumes = [arry objectEnumerator];
id obj;
while (obj = [Enumes nextObject]) {
NSLog(@"%@", obj);
}
//快速枚举法
for(id obj in arry) {
NSLog(@"%@", obj);
}
//使用i值遍历
NSUInteger length = [arry count];
NSLog(@"%li", length);
for(int i = 0; i < length; i ++) {
NSLog(@"%@", [arry objectAtIndex: i]);
}
//不可变数组 NSMutableArray
//NSMutableArray *mutableArray = [NSMutableArray arrayWithObjects: @"a", @"b", @"c", nil];
NSMutableArray *mutableArray = [NSMutableArray new];
// 增加元素
[mutableArray addObject: @"dog"];
[mutableArray addObject: @"cat"];
[mutableArray addObject: @"snake"];
NSLog(@"%@", mutableArray);
//删除元素, 如果有两个相同的元素, 这个操作会两个同时删除
[mutableArray removeObject: @"dog"];
//根据index去删除元素
[mutableArray removeObjectAtIndex: 1 ];
NSLog(@"%@", mutableArray);
//new mutable arry
NSMutableArray *colorArray = [NSMutableArray new];
[colorArray addObject:@"red"];
[colorArray addObject:@"yellow"];
[colorArray addObject:@"green"];
[colorArray addObject:@"black"];
//交换元素位置
// [colorArray exchangeObjectAtIndex:0 withObjectAtIndex:1 ];
//用枚举法的时候, 只能够在数组倒叙的情况下修改数组
NSEnumerator *enumeArray = [colorArray reverseObjectEnumerator];
NSString *str;
while (str = [enumeArray nextObject]) {
NSLog(@"%@", str);
}
// 字符串转化为数组
NSString *Str = @"Hi i am facial";
//把字符串变成数组, 使用componentsSeparatedByString
NSArray *testarry = [Str componentsSeparatedByString: @" "];
NSEnumerator * StrEnumer = [testarry reverseObjectEnumerator];
NSMutableArray * ReverseArray = [NSMutableArray new];
NSString *str_temp;
while (str_temp = [StrEnumer nextObject]) {
[ReverseArray addObject: str_temp];
}
//把数组变成字符串, 使用componentsJoinedByString
NSString *newStr = [ReverseArray componentsJoinedByString: @" "];
NSLog(@"%@", newStr);
}
return 0;
}
// main.m
// Array
//
// Created by facial on 23/8/15.
// Copyright (c) 2015 facial_huo. All rights reserved.
//
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
NSLog(@"Hello, World!");
//初始化一个不可变数组
NSArray *arry = [NSArray arrayWithObjects:@"one one", @"two", @"three", nil ];
NSLog(@"%@", arry);
//打印数组方法
//枚举器法
NSEnumerator * Enumes = [arry objectEnumerator];
id obj;
while (obj = [Enumes nextObject]) {
NSLog(@"%@", obj);
}
//快速枚举法
for(id obj in arry) {
NSLog(@"%@", obj);
}
//使用i值遍历
NSUInteger length = [arry count];
NSLog(@"%li", length);
for(int i = 0; i < length; i ++) {
NSLog(@"%@", [arry objectAtIndex: i]);
}
//不可变数组 NSMutableArray
//NSMutableArray *mutableArray = [NSMutableArray arrayWithObjects: @"a", @"b", @"c", nil];
NSMutableArray *mutableArray = [NSMutableArray new];
// 增加元素
[mutableArray addObject: @"dog"];
[mutableArray addObject: @"cat"];
[mutableArray addObject: @"snake"];
NSLog(@"%@", mutableArray);
//删除元素, 如果有两个相同的元素, 这个操作会两个同时删除
[mutableArray removeObject: @"dog"];
//根据index去删除元素
[mutableArray removeObjectAtIndex: 1 ];
NSLog(@"%@", mutableArray);
//new mutable arry
NSMutableArray *colorArray = [NSMutableArray new];
[colorArray addObject:@"red"];
[colorArray addObject:@"yellow"];
[colorArray addObject:@"green"];
[colorArray addObject:@"black"];
//交换元素位置
// [colorArray exchangeObjectAtIndex:0 withObjectAtIndex:1 ];
//用枚举法的时候, 只能够在数组倒叙的情况下修改数组
NSEnumerator *enumeArray = [colorArray reverseObjectEnumerator];
NSString *str;
while (str = [enumeArray nextObject]) {
NSLog(@"%@", str);
}
// 字符串转化为数组
NSString *Str = @"Hi i am facial";
//把字符串变成数组, 使用componentsSeparatedByString
NSArray *testarry = [Str componentsSeparatedByString: @" "];
NSEnumerator * StrEnumer = [testarry reverseObjectEnumerator];
NSMutableArray * ReverseArray = [NSMutableArray new];
NSString *str_temp;
while (str_temp = [StrEnumer nextObject]) {
[ReverseArray addObject: str_temp];
}
//把数组变成字符串, 使用componentsJoinedByString
NSString *newStr = [ReverseArray componentsJoinedByString: @" "];
NSLog(@"%@", newStr);
}
return 0;
}
相关文章推荐
- php get_object_vars()函数
- 【JavaSE】day02_正则表达式 、 Object 、 包装类
- Objective-C 【类对象及SEL存储方式】
- Objective-C 【类对象及SEL存储方式】
- Objective-C 【多态】
- Objective-C 【多态】
- Spring Object/XML mapping example
- Spring Object/XML mapping example
- Objective-C中,ARC下的 strong和weak指针原理解释,objective-cweak
- mysql_fetch_array(),mysql_fetch_object,mysql_fetch_rows区别
- org.json.JSONException: A JSONObject text must begin with '{' at character 1 of {解决方法
- Objective-C关于协议protocol与代理degelate以及protocol的类型限制
- 内部类,异常,Object类
- JSONObject使用方法
- Objective-C 【继承、变量修饰符(私有变量/方法)、description方法】
- Objective-C 【继承、变量修饰符(私有变量/方法)、description方法】
- Effective Objective-C 2.0学习笔记(部分)
- Object-C @synthesize -- 笔记
- Object -C @property -- 笔记
- Object-C 点语法 -- 笔记