哪一个是更有效? :循环效率对比
ArrayList<Integer> list = new ArrayList<Integer>();
for(int a : list){
log.i(tag, a + "");
}
SparseIntArray list2 = new SparseIntArray();
int count = list2.size();
for(int j = 0; j < count; j++) {
log.i(tag, list2.get(j) + "");
}
或者,有没有阅读列表的内容更快的方法?
检查:http://developer.android.com/guide/practices/design/performance.html#foreach – idiottiger 2012-03-13 03:20:35
请问是否有指标之间的差距,第二个甚至工作?文档说'list2.size()'返回键/值对的数量,而不是最高的索引。意思是,如果你的地图有两个条目({100 => 100,200 => 200}),你不会看到它们吗? – cHao 2012-03-13 03:21:51
@cHao正确,也不一定会遍历所有的值(除非前面的代码写这样一来,这不是强制执行的),另外,在范围内的任何丢失的钥匙将只返回0,这意味着log.i(标签,“ 0“)可能会被称为很多。 – Chet 2012-03-13 03:34:11