test() async { Completer c = new Completer(); for (var i = 0; i < 1000; i++) { if (i == 900 && c.isCompleted == false) { c.completeError('error in $i'); } if (i == 800 && c.isCompleted == false) { c.complete('complete in $i'); } }
try { String res = await c.future; print(res); //得到complete传入的返回值 'complete in 800' } catch (e) { print(e);//捕获completeError返回的错误 } }
怎么将一个Callback回调转化成Future同步方法(Callback to Future),可以配套async / await去使用呢?
Here are main question we will ask during interview: 1.Data Structure: a.linked lists b.Stacks c.Queues d.binary trees e.hash tables 2.Algorithms: a.Sort (like quick sort and merge sort), binary search, greedy algorithms. b.Binary tree(like traversal, construction, manipulation..) You can find more to do exercises on https://leetcode.com/
- (void)methodMaopao { int array[5] = {4, 5, 1, 3, 2}; for (int i = 0; i < 5-1; i++) { //需要比较几遍 for(int j = 0; j < 5-1-i; j++) { //每一遍都从0开始到倒数第-i个 if (array[j] > array [j + 1]){ int temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } } }
for(int i = 0; i < 5; i++) { printf("%d\n",array[i]); } }
2、选择排序代码实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
funcsort(items: Array<Int>) -> Array<Int> { var list = items for i in0..<list.count { //记录当前最小的数,比较i+1后更大的数进行记录 var minIndex = i for j in i+1..<list.count { if list[j] < list[minIndex] { minIndex = j } } // 交换 let temp = list[minIndex] list[minIndex] = list[i] list[i] = temp } return list }