Preface
Some people say that before for loop, a local variable is used to get list.size(), str.length(), and then in the judgment condition of for loop, the local variable is used to replace list.size(), str.length() to save the time of data calculation. Is that really the case? Now I will answer this question for you.
List.size()
First, let's look at the List interface. We know that the. size() method is a method of the List interface, returning an int value.
public interface List<E> extends Collection<E> {
//Omit part of the code...
/**
* Returns the number of elements in this {@code List}.
*
* @return the number of elements in this {@code List}.
*/
public int size();
//Omit part of the code...
}
The methods in the interface are not specifically implemented. Let's take a look at the implementation class ArrayList of List (LinkList is the same, ArrayList here). Let's first look at how the size() method in the ArrayList class is implemented:
public class ArrayList<E> extends AbstractList<E> implements Cloneable, Serializable, RandomAccess {
//Eliminate part of the code.
/**
* Returns the number of elements in this {@code ArrayList}.
*
* @return the number of elements in this {@code ArrayList}.
*/
@Override public int size() {
return size;
}
//Eliminate part of the code.
}
We see that the size() method in the ArrayList return s a size directly. By looking at it, we find that size is a member variable of type int in the ArrayList class, representing the number of elements in the list combination.
/**
* The number of elements in this list.
*/
int size;
By tracking the size variable, it is found that add in the ArrayList class changes the size of size dynamically in the remove method.
/**
* 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; // Adding element size increases
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); //Delete the element size--
a[s] = null; // Prevent memory leak
size = s;
modCount++;
return result;
}
From the above code, we know that getting the length of a collection through the. size() method in ArrayList will directly return the variable value of the number of elements in a collection, instead of recalculating the number of elements in the lower set every time the size() method is called. Let's look at String.length().
String.Length()
Let's look at the String class under the java.lang package and first find the. length() method in the String class:
/**
* 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 {
//Eliminate part of the code.
private final int count;
//Eliminate part of the code.
/**
* 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;
}
//Eliminate part of the code.
}
We found that, like the size() method in ArrayList, a member variable count of type int was returned. I don't know how to assign this count. If you are interested, you can study it.
summary
In summary, we can see that List.size() and String.length() methods both return an int variable value directly, instead of taking time to calculate the size and return, so feel free to use size() and length() methods.