陣列操作之遍歷,排序,逆序

2022-08-30 01:42:13 字數 863 閱讀 4266

陣列遍歷:

索引遍歷

列舉遍歷:

nsenumerator* enumerator = [arrayobjectenumerator];  

id e =nil;  

while (e = [enumeratornextobject])  

**塊遍歷:

// 示例1:列舉遍歷  

[arrayenumerateobjectsusingblock:^ (id obj, nsuinteger idx, boolbool *stop)];  

// 示例2:列舉遍歷,遇到符合條件的元素(obj=array[idx])即退出遍歷。  

[arrayenumerateobjectsusingblock:^ (id obj, nsuinteger idx, boolbool *stop) else   

}]; 

陣列排序:

迴圈排序

**塊排序:

nsarray *array = @[@12,@23,@8];

nscomparator sortblock = ^(id string1,id string2)else if ([string1 floatvalue]==[string2 floatvalue])else{

return nsordereddescending;

nsarray *sortarray = [array sortedarrayusingcomparator:sortblock];

nslog(@"%@",sortarray);

陣列逆序:

遍歷逆序

方法逆序:

[[array reverseobjectenumerator] allobjects];

陣列逆序操作

陣列逆序操作 定義長度為10的陣列,將陣列元素對調,並輸出對調前後的結果。思路 把0索引和arr.length 1的元素交換,把1索引和arr.length 2的元素交換 只要交換到arr.length 2的時候即可。public class a 定義乙個陣列並進行靜態初始化 逆序前 system....

陣列氣泡排序,選擇排序,逆序,折半查詢,遍歷方法

public class hello2 呼叫方法是有先後順序的 xuanze arr maopao arr nixu arr bianli arr int index zheban arr,5 system.out.println index 折半查詢方法 其陣列必須是有序資料從小到達排列 publ...

陣列遍歷排序

集合的遍歷 nsset 集合 nsdictionary dictionary nsdictionary dictionarywithobjectsandkeys 11 a 22 b 33 c nil nslog dictionary for int i 0 i dictionary.count i ...