Android Studio For Experts

自动完成 auto completion ctrl + space 选择后按 tab 可以覆盖原理的方法。 shift + ctrl + space 更加聪明的补全方式 selection extend selection alt + ↑ intention alt + enter 对于构造函数,可以自动生成private 变量并赋值,增加变量可自动添加。 在 instanceCheck 中可以自动生成 cast 代码 suppress templates fori list.for 自动生成循环 logi logd loge 打印 logt 生成 TAG logm 方法开头用 logr 方法结尾用 command + n 弹出Generate 布局 tools:showIn="@layout/fragment_sign_in" 可以包含另一个布局来预览 tools 关键字可以用在不同的参数上,用上后该参数只会在预览中有效。 public.xml 可以暴露想暴露的属性给 ide 格式为 <resources> <public name="ccl_app_name" type="string"/> </resources> ctrl + tab Switcher How to use debugger To find an action ...

November 26, 2015

Priority Queues

定义 不同于 Stack 的 FIFO 和 Queue 的 FILO。Priority Queue 总是删除集合中最大(或最小)的数。例如 operation argument return value insert P insert Q insert E remove max Q insert X insert A insert M remove max X insert P insert L insert E remove max P 如果使用数组来实现 Priority Queue 的插入和删除操作,需要的时间复杂度和理想的时间复杂度如下 implementation insert del max max unorded array 1 N N ordered array N 1 1 goal logN logN logN 有一种数据结构可以实现目标的时间复杂度,我们叫它做二叉堆 ...

September 29, 2015

Tree

BST BST 的特性就是左子树都比本节点小,右子树都比本节点大。 private class Node { private Key key; private Value val; private Node left, right; public Node(Key key, Value val) { this.key = key; this.val = val; } } public class BST<Key extends Comparable<Key>, Value> { private Node root; public void put(Key key, Value val) { root = put(root, key, val); } private Node put(Node x, Key key, Value val) { if (x == null) return new Node(key, val); int cmp = key.comapreTo(x.key); if (cmp < 0) x.left = put(x.left, key, val); else if (cmp > 0) x.right = put(x.right, key, val); else x.val = val; return x; } public Value get(Key key) { Node x = root; while(x != null) { int cmp = key.comapreTo(x.key); if (cmp < 0) x = x.left; else if (cmp > 0) x = x.right; else return x.val; } return null; } public void delete(Key key) { root = delete(root, key); } private Node delete(Node x, Key key) { if (x == null) return null; int cmp = key.comapreTo(x.key); if (cmp < 0) x.left = delete(x.left, key); else if (cmp > 0) x.right = delete(x.right, key); else { if (x.right == null) return x.left; if (x.left == null) return x.right; Node t = x; x = min(t.right); x.right = deleteMin(t.right); x.left = t.left; } x.count = size(x.left) + size(x.right) + 1; return x; } public Iterable<Key> iterator { Queue<Key> q = new Queue<Key>(); inorder(root, q); return q; } private void inorder(Node x, Queue<Key> q) { if (x == null) return; inorder(x.left, q); q.enqueue(x.key); inorder(x.right, q); } } floor 寻找比 Key 小的数中最大的数 ...

August 20, 2015

Sort

Selection sort In iteration i, find index min of smallest remaining entry. Swap a[i] and a[min] i 从0开始,找到数组中最小的,与i交换,i++,直到 i 等于数组长度。 Comparable[] a; int N = a.length; for(int i = 0; i < N; i++) { int min = i; for(int j = i + 1; j < N; j++) { if(less(a[j], a[min])) min = j; } exch(a, i, min); } Both best case and worst case: ...

August 9, 2015

LRU cache in Android

曾经我们使用 WeakReference 来处理 Android 中 Out of Memory 的问题。但是随着 Google 修改了回收机制的行为,这个方法已经没什么用了。 因此,Google 为我们提供了一个新的数据结构,LRU cache。 它的使用是这样的 LruCache bitmapCache = new LruCache<String, Bitmap>() 像是使用 Map ,但是它有一些独特的行为特性。 使用前,我们需要确定 cache 的大小。如何确定需要多大的空间? Google 给了我们一个最佳实践的空间计算方法。g ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); int availMemInBytes = am.getMemoryClass() * 1024 * 1024; LruCache bitmapCache = new LruCache<String, Bitmap>(availMemInBytes / 8); 另外还有一个重要的部分是我们需要告诉 cache 每个 bitmap 的大小,如何告诉?重写 sizeOf 方法 public class ThumbnailCache extends LruCache(String, Bitmap) { @Override protected int sizeOf(String key, Bitmap value) { return value.getByteCount(); } } 前面提到过使用 cache 像是使用 Map。但是这个 cache 可能会返回 null,表示 cache 中找不到我们需要的文件,这时候我们就需要从其他地方载入图片然后把它塞进 cache 中。 ...

June 8, 2015