在循环遍历对象之前预先计算并保存数组的长度是否是更好的编码实践?同样,对于字符串呢?
<div id="1">asdf</div>
vs
//Given int Array A, that won't change size
for (int i = 0; i < A.length; i++)
System.out.println(A[i]);
- 类似于字符串
int Asize = A.length;
for (int i = 0; i < Asize; i++)
System.out.println(A[i]);
vs
//Given string s, that won't change size
for (int i = 0; i < s.length(); i++)
System.out.println(s.charAt(i));