我应该在循环对象之前保存对象的长度吗?

时间:2018-02-16 19:27:54

标签: java

在循环遍历对象之前预先计算并保存数组的长度是否是更好的编码实践?同样,对于字符串呢?

<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));

0 个答案:

没有答案