有人說(shuō)在for循環(huán)之前用一個(gè)局部變量先獲取到list.size()、str.length(),然后在for循環(huán)的判斷條件里通過(guò)這個(gè)局部變量替換list.size()、str.length()會(huì)節(jié)省數(shù)據(jù)計(jì)算的時(shí)間。事實(shí)真的是這樣嗎?下面就為大家解答這個(gè)問(wèn)題。
說(shuō)明:此文章針對(duì)Android SDK 進(jìn)行說(shuō)明。
List.size()
首先我們看一下List接口,我們知道.size()方法是List接口的一個(gè)方法,返回一個(gè)int類(lèi)型的值。
public interface List<E> extends Collection<E> { //省略部分代碼... /** * Returns the number of elements in this {@code List}. * * @return the number of elements in this {@code List}. */ public int size(); //省略部分代碼... }
接口中的方法都是沒(méi)有具體實(shí)現(xiàn)的,我們下面看一下List的實(shí)現(xiàn)類(lèi)ArrayList(LinkList也一樣,這里講ArrayList)。我們先看下ArrayList類(lèi)中的size()方法是如何實(shí)現(xiàn)的:
?
public class ArrayList<E> extends AbstractList<E> implements Cloneable, Serializable, RandomAccess { //省略部分代碼... /** * Returns the number of elements in this {@code ArrayList}. * * @return the number of elements in this {@code ArrayList}. */ @Override public int size() { return size; } //省略部分代碼... }
我們看到ArrayList里的size()方法直接return了一個(gè)size,通過(guò)查看發(fā)現(xiàn)size是ArrayList類(lèi)中的一個(gè)int類(lèi)型的成員變量,代表list結(jié)合中的元素?cái)?shù)量。
?
/** * The number of elements in this list. */ int size;
通過(guò)跟蹤size變量發(fā)現(xiàn)在ArrayList類(lèi)中的add,remove方法中都會(huì)動(dòng)態(tài)改變size的大小。
?
/** * Adds the specified object at the end of this {@code ArrayList}. * * @param object * the object to add. * @return always true */ @Override public boolean add(E object) { Object[] a = array; int s = size; if (s == a.length) { Object[] newArray = new Object[s + (s < (MIN_CAPACITY_INCREMENT / 2) ? MIN_CAPACITY_INCREMENT : s >> 1)]; System.arraycopy(a, 0, newArray, 0, s); array = a = newArray; } a[s] = object; size = s + 1; // 添加元素size增加 modCount++; return true; } ... /** * Removes the object at the specified location from this list. * * @param index * the index of the object to remove. * @return the removed object. * @throws IndexOutOfBoundsException * when {@code location < 0 || location >= size()} */ @Override public E remove(int index) { Object[] a = array; int s = size; if (index >= s) { throwIndexOutOfBoundsException(index, s); } @SuppressWarnings("unchecked") E result = (E) a[index]; System.arraycopy(a, index + 1, a, index, --s - index); //刪除元素 size-- a[s] = null; // Prevent memory leak size = s; modCount++; return result; }
通過(guò)上述代碼我們知道通過(guò)ArrayList中的.size()方法獲取集合長(zhǎng)度,會(huì)直接返回一個(gè)集合元素?cái)?shù)量的變量值,而不會(huì)每次調(diào)用size()方法都重新計(jì)算下集合的元素?cái)?shù)量再返回。下面我們?cè)诳聪耂tring.length()。
String.Length()
我們看下java.lang包下得String類(lèi),首先找到String類(lèi)中的.length()方法:
/** * An immutable sequence of UTF-16 {@code char}s. * See {@link Character} for details about the relationship between {@code char} and * Unicode code points. * * @see StringBuffer * @see StringBuilder * @see Charset * @since 1.0 */ public final class String implements Serializable, Comparable<String>, CharSequence { //省略部分代碼... private final int count; //省略部分代碼... /** * Returns the number of {@code char}s in this string. If this string contains surrogate pairs, * this is not the same as the number of code points. */ public int length() { return count; } //省略部分代碼... }
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-702570.html
我們發(fā)現(xiàn)跟ArrayList中的size()方法一樣,返回了一個(gè)int類(lèi)型的成員變量count。這個(gè)count是怎么賦值的我也不清楚,有興趣的可以去研究一下。
#總結(jié)
綜上所述,我們就可以知道List.size()和String.length()方法都是直接返回一個(gè)int類(lèi)型變量值,而不會(huì)花費(fèi)時(shí)間再去計(jì)算大小后返回,所以放心的去使用size()和length()方法吧。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-702570.html
到了這里,關(guān)于Java for循環(huán)每次都通過(guò)list.size()和 string.length()獲取大小性能的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!