一.数组
NSArray
用来存储有序的列表,他是不可变的,一旦装入10个元素就不能再添加删除元素
不能存储基本类型,只允许装OC对象,结构体,枚举,nil都不行,nil代表数组的结束
数组遍历的代码:
-
//
-
// Student.m
-
// 遍历
-
//
-
// Created by 金海洋 on 15-4-3.
-
// Copyright (c) 2015年 金海洋. All rights reserved.
-
//
-
-
#import "Student.h"
-
-
@implementation Student
-
-
- (void)dealloc
-
{
-
-
NSLog(@"student销毁了");
-
[super dealloc];
-
}
-
-
+(id)stu{
-
//快速创建对象,不用管理内存
-
Student *st = [[[Student alloc]init]autorelease];
-
return st;
-
-
}
-
-
-(void)fun{
-
-
NSLog(@"fun");
-
}
-
-
-(void)funa:(id)a{
-
-
NSLog(@"fun2--%@",a);
-
}
-
-
@end
-
//
-
// main.m
-
// 遍历
-
//
-
// Created by 金海洋 on 15-4-3.
-
// Copyright (c) 2015年 金海洋. All rights reserved.
-
//
-
-
#import <Foundation/Foundation.h>
-
#import "Student.h"
-
-
//内存管理
-
void memory(){
-
-
Student *s1 = [[Student alloc] init];
-
Student *s2 = [[Student alloc] init];
-
Student *s3 = [[Student alloc] init];
-
-
NSLog(@"s1--1--%zd",[s1 retainCount]);
-
//当把对象塞进数组时候,这个对象计数器就加1
-
NSArray *n1 = [[NSArray alloc]initWithObjects:s1,s2,s3, nil];
-
-
NSLog(@"s1--2--%zd",[s1 retainCount]);
-
-
NSLog(@"cont--%zd",n1.count);
-
//当数组元素销毁的时候,他都会对里面的对象release
-
[n1 release];
-
[s1 release];
-
[s2 release];
-
[s3 release];
-
-
}
-
-
//给数组里面的元素发送消息
-
void fun1(){
-
-
Student *s1 = [Student stu];
-
Student *s2 = [Student stu];
-
Student *s3 = [Student stu];
-
NSArray *n1 = [NSArray arrayWithObjects:s1,s2,s3, nil];
-
NSString *a =@"123";
-
-
[n1 makeObjectsPerformSelector:@selector(fun)];
-
[n1 makeObjectsPerformSelector:@selector(funa:) withObject:a];
-
-
}
-
-
//遍历
-
void fun2(){
-
-
NSArray *n1 = [NSArray arrayWithObjects:@"12",@"23",@"2", nil];
-
int cont = n1.count;
-
for(int i =0 ; i<cont;i++){
-
//id == *void
-
id obj = [n1 objectAtIndex:i ];
-
NSLog(@"%@",obj);
-
-
}
-
-
}
-
-
//遍历2
-
void fun3(){
-
-
NSArray *n1 = [NSArray arrayWithObjects:@"12",@"23",@"2", nil];
-
//快速遍历
-
for(id obj in n1){
-
NSLog(@"%@",obj);
-
-
}
-
-
-
}
-
-
//遍历3
-
void fun4(){
-
//用block来遍历
-
NSArray *n1 = [NSArray arrayWithObjects:@"12",@"23",@"2", nil];
-
//创建的时候双击就会自动生成
-
[n1 enumerateObjectsUsingBlock:
-
^(id obj, NSUInteger idx, BOOL *stop) {
-
-
//如果索引为1,就停止遍历
-
if ( idx == 1) {
-
//stop为yes的时候,会停止遍历
-
*stop = YES;
-
}
-
-
NSLog(@"%@",obj);
-
-
-
}];
-
-
-
-
}
-
-
//遍历4
-
void fun5(){
-
-
//迭代器
-
NSArray *n1 = [NSArray arrayWithObjects:@"12",@"23",@"2", nil];
-
//获取数组的迭代器
-
NSEnumerator *e = [n1 objectEnumerator];
-
-
//返回迭代器所有元素
-
//allObjects 只取出没有被遍历过的对象
-
NSArray *ns = [e allObjects];
-
-
NSLog(@"%@",ns);
-
-
e = [n1 objectEnumerator];
-
//获取下一个需要遍历的元素
-
//[e nextObject];
-
id a = nil;
-
while (a = [e nextObject]) {
-
-
-
NSLog(@"--%@",a);
-
}
-
-
//获取反序的迭代器
-
e = [n1 reverseObjectEnumerator];
-
-
id a1 = nil;
-
while (a1 = [e nextObject]) {
-
-
-
NSLog(@"=%@",a1);
-
}
-
-
-
}
-
-
int main(int argc, const char * argv[]) {
-
@autoreleasepool {
-
//创建一个空数组
-
NSArray *a1 = [NSArray array];
-
//创建多个元素的数组
-
NSArray *a2 = [NSArray arrayWithObjects:@"123",@"sdf",@"abc",nil];
-
-
//获取元素个数
-
unsigned int cout = [a2 count];
-
unsigned int cout1 = a2.count;
-
-
NSLog(@"个数为%zd",cout);
-
-
//查看包含字符串
-
if([a2 containsObject:@"abc"]){
-
NSLog(@"包含了");
-
}
-
-
//返回最后一个元素
-
NSString *s = [a2 lastObject];
-
NSLog(@"最后一个元素-%@",s);
-
-
//根据下标获取元素
-
s = [a2 objectAtIndex:2];
-
NSLog(@"元素为-%@",s);
-
-
//查找索引
-
int index = [a2 indexOfObject:@"123"];
-
NSLog(@"索引为-%d",index);
-
-
//memory();
-
//fun1();
-
//fun2();
-
//fun3();
-
//fun4();
-
fun5();
-
-
}
-
return 0;
-
}
下面是数组的排序:
-
//
-
// Student.h
-
// 排序
-
//
-
// Created by 金海洋 on 15-4-3.
-
// Copyright (c) 2015年 金海洋. All rights reserved.
-
//
-
-
#import <Foundation/Foundation.h>
-
@class Book;
-
-
@interface Student : NSObject
-
-
-
@property (nonatomic,retain)NSString *name;
-
@property (nonatomic,retain)NSString *xing;
-
@property (nonatomic,retain)Book *book;
-
-
+(id)stu1:(NSString*)s1 stu2:(NSString*)s2;
-
+(id)stu1:(NSString*)s1 stu2:(NSString*)s2 bo:(NSString*)book;
-
-
-(NSComparisonResult)comp:(Student*)s;
-
-
@end
-
//
-
// Student.m
-
// 排序
-
//
-
// Created by 金海洋 on 15-4-3.
-
// Copyright (c) 2015年 金海洋. All rights reserved.
-
//
-
-
#import "Student.h"
-
#import "Book.h"
-
-
@implementation Student
-
-
+(id)stu1:(NSString*)s1 stu2:(NSString*)s2{
-
-
Student *s = [[Student alloc]init];
-
s.name = s1;
-
s.xing = s2;
-
-
return s;
-
-
}
-
-
+(id)stu1:(NSString*)s1 stu2:(NSString*)s2 bo:(NSString *)book{
-
-
Student *s = [[Student alloc]init];
-
s.name = s1;
-
s.xing = s2;
-
s.book = [Book bookWithName:book];
-
-
return s;
-
-
}
-
-
-(NSComparisonResult)comp:(Student*)s{
-
-
-
//先按照姓名排序
-
NSComparisonResult re = [self.xing compare:s.xing];
-
//名字一样的话,按照名字排序
-
if (re == NSOrderedSame){
-
-
re = [self.name compare:s.name];
-
-
}
-
-
return re ;
-
}
-
-
- (NSString *)description
-
{
-
return [NSString stringWithFormat:@"%@--%@--%@", self.xing,self.name,self.book.name];
-
}
-
-
-(void)dealloc{
-
-
[_name release];
-
[_xing release];
-
[_book release];
-
[super release];
-
-
}
-
-
@end
-
//
-
// Book.h
-
// 排序
-
//
-
// Created by 金海洋 on 15-4-14.
-
// Copyright (c) 2015年 金海洋. All rights reserved.
-
//
-
-
#import <Foundation/Foundation.h>
-
-
@interface Book : NSObject
-
-
-
@property (nonatomic,retain)NSString *name;
-
-
+(id)bookWithName:(NSString*)name;
-
-
@end
-
//
-
// Book.m
-
// 排序
-
//
-
// Created by 金海洋 on 15-4-14.
-
// Copyright (c) 2015年 金海洋. All rights reserved.
-
//
-
-
#import "Book.h"
-
-
@implementation Book
-
-
+(id)bookWithName:(NSString*)name{
-
-
Book *b = [[[Book alloc]init]autorelease];
-
-
b.name = name;
-
-
return b;
-
-
}
-
-
- (void)dealloc
-
{
-
[_name release];
-
[super release];
-
-
}
-
-
@end
-
//
-
// main.m
-
// 排序
-
//
-
// Created by 金海洋 on 15-4-3.
-
// Copyright (c) 2015年 金海洋. All rights reserved.
-
//
-
-
#import <Foundation/Foundation.h>
-
#import "Student.h"
-
-
//派生出新的数组
-
void fun1(){
-
-
NSArray *n1 = [NSArray arrayWithObjects:@"123",@"23",@"222",nil ];
-
-
NSArray *n2 = [n1 arrayByAddingObject:@"1231"];
-
NSArray *n3 =[n1 arrayByAddingObjectsFromArray:[NSArray arrayWithObjects:@"123",@"23",@"222",nil ]];
-
NSRange r = NSMakeRange(1, 2);
-
//截取后派生
-
NSArray *n4 =[n3 subarrayWithRange:r];
-
-
NSLog(@"n1--%zd",n1.count);
-
NSLog(@"n2--%zd",n2.count);
-
NSLog(@"n3--%zd",n3.count);
-
NSLog(@"n4--%zd",n4.count);
-
-
}
-
-
//拼接
-
void fun2(){
-
-
NSArray *n1 = [NSArray arrayWithObjects:@"123",@"23",@"222",nil ];
-
//用 -- 拼接元素
-
NSString *s = [n1 componentsJoinedByString:@"--"];
-
NSLog(@"%@",s );
-
//123--23--222
-
-
NSString *s1 = @"/Users/jinhaiyang/Desktop/33.txt";
-
//将数组内容以xml形式写进文件
-
[n1 writeToFile:s1 atomically:YES];
-
-
NSString *s2 = @"/Users/jinhaiyang/Desktop/44.txt";
-
//从文件读取内容,文件有严格要求
-
NSArray *n2 = [NSArray arrayWithContentsOfFile:s2];
-
NSLog(@"%@",n2);
-
-
}
-
-
//排序
-
void fun3(){
-
-
NSArray *n1 = [NSArray arrayWithObjects:@"123",@"23",@"222",nil ];
-
//返回一个排好序的数组,原来数组不变
-
//compare 告诉它这个数组的比较方法 从小到大
-
NSArray *n2 = [n1 sortedArrayUsingSelector:@selector(compare:)];
-
NSLog(@"%@",n2);
-
-
}
-
-
//排序2
-
//自己写比较方法
-
void fun4(){
-
-
Student *s = [Student stu1:@"li" stu2:@"sun"];
-
Student *s1 = [Student stu1:@"ang" stu2:@"li"];
-
Student *s2 = [Student stu1:@"gang" stu2:@"li"];
-
-
NSArray *n1 = [NSArray arrayWithObjects:s,s1,s2,nil ];
-
NSArray *n2 = [n1 sortedArrayUsingSelector:@selector(comp:)];
-
-
NSLog(@"%@",n2);
-
}
-
-
//排序3
-
void fun5(){
-
//block
-
Student *s = [Student stu1:@"li" stu2:@"sun"];
-
Student *s1 = [Student stu1:@"ang" stu2:@"li"];
-
Student *s2 = [Student stu1:@"gang" stu2:@"li"];
-
-
NSArray *n1 = [NSArray arrayWithObjects:s,s1,s2,nil ];
-
-
NSArray *n2 = [n1 sortedArrayUsingComparator:
-
^NSComparisonResult(Student *s1, Student *s2) {
-
-
//先按照姓名排序
-
NSComparisonResult re = [s1.xing compare:s2.xing];
-
//名字一样的话,按照名字排序
-
if (re == NSOrderedSame){
-
-
re = [s1.name compare:s2.name];
-
-
}
-
-
return re ;
-
-
}];
-
-
NSLog(@"%@",n2);
-
-
}
-
-
//排序4
-
void fun6(){
-
-
Student *s = [Student stu1:@"li" stu2:@"sun" bo:@"abc"];
-
Student *s1 = [Student stu1:@"ang" stu2:@"li" bo:@"sf"];
-
Student *s2 = [Student stu1:@"gang" stu2:@"li" bo:@"abc"];
-
-
NSArray *n1 = [NSArray arrayWithObjects:s,s1,s2,nil ];
-
-
//先创造一个排序描述器
-
//ascending 默认是升序
-
//sortDescriptorWithKey: 写上去的是 property后面声明的名字
-
NSSortDescriptor *a = [NSSortDescriptor sortDescriptorWithKey:@"book.name" ascending:YES];
-
NSSortDescriptor *b = [NSSortDescriptor sortDescriptorWithKey:@"xing" ascending:YES];
-
NSSortDescriptor *c = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
-
-
//按照要先进行排序的属性,放进数组里
-
NSArray *n2 = [NSArray arrayWithObjects:a,b,c ,nil ];
-
-
NSArray *n3 = [n1 sortedArrayUsingDescriptors:n2];
-
NSLog(@"%@",n3);
-
-
-
}
-
-
int main(int argc, const char * argv[]) {
-
@autoreleasepool {
-
-
//fun1();
-
//fun2();
-
//fun3();
-
//fun4();
-
//fun5();
-
fun6();
-
-
}
-
return 0;
-
}
二.动态数组
可变数组
不可以添加空和基本类型
动态数组创建的代码:
-
//
-
// main.m
-
// 可变数组
-
//
-
// Created by 金海洋 on 15-4-8.
-
// Copyright (c) 2015年 金海洋. All rights reserved.
-
//
-
-
#import <Foundation/Foundation.h>
-
#import "Student.h"
-
-
-
void mem(){
-
//内存管理
-
//NSMutableArray *n = [NSMutableArray array];
-
NSMutableArray *n = [[NSMutableArray alloc]init];
-
-
Student *s1 = [Student StudentWithAge:34];
-
Student *s2 = [Student StudentWithAge:14];
-
Student *s3 = [Student StudentWithAge:100];
-
-
//会给被添加的元素进行一次retain操作
-
[n addObject:s1];
-
[n addObject:s2];
-
[n addObject:s3];
-
NSLog(@"%zd",[s1 retainCount]);
-
//会给被添加的元素进行一次release 操作
-
[n removeObject:s1];
-
NSLog(@"%zd",[s1 retainCount]);
-
[n addObject:s2];
-
-
//数组释放的时候都会对元素做一次release操作
-
[n release];
-
-
}
-
-
void fun(){
-
//删除
-
-
NSArray *ns1= [NSMutableArray arrayWithObjects:@"1", @"2",@"112", @"212",@"412",nil];
-
//删除index位置的元素
-
//[ns1 removeObjectAtIndex:1];
-
//删除NSRange所指范围的元素
-
NSRange r = NSMakeRange(0, 2);
-
[ns1 removeObjectsInRange:r];
-
-
NSLog(@"%@",ns1);
-
-
-
-
}
-
-
void fun1(){
-
//替换
-
NSMutableArray *ns1 = [NSMutableArray arrayWithObjects:@"1", @"2",@"112",nil];
-
-
[ns1 replaceObjectAtIndex:1 withObject:@"4444"];
-
-
NSLog(@"%@",ns1);
-
-
-
}
-
-
void fun2(){
-
//排序
-
NSMutableArray *ns1 = [NSMutableArray arrayWithObjects:@"11", @"2",@"112",nil];
-
//返回排序号的数组
-
NSMutableArray *ns2 = [ns1 sortedArrayUsingSelector:@selector(compare:)];
-
//直接在这个数组里排序
-
[ns1 sortUsingSelector:@selector(compare:)];
-
-
NSLog(@"%@",ns1);
-
NSLog(@"---%@",ns2);
-
-
}
-
-
int main(int argc, const char * argv[]) {
-
@autoreleasepool {
-
-
NSMutableArray *ns1 = [NSMutableArray arrayWithObjects:@"1", @"2",@"112",nil];
-
-
//动态添加元素
-
[ns1 addObject:@"123"];
-
NSLog(@"%@",ns1);
-
//删除对象@"1"
-
[ns1 removeObject:@"1"];
-
NSLog(@"%@",ns1);
-
//删除最后一个对象
-
[ns1 removeLastObject];
-
//删除所有对象
-
[ns1 removeAllObjects];
-
NSLog(@"%@",ns1);
-
-
//mem();
-
fun();
-
//fun1();
-
//fun2();
-
-
}
-
return 0;
-
}
阅读(750) | 评论(0) | 转发(0) |