Xem the documentation for ArrayList#remove(int)
, như trong cú pháp sau:
list.remove(list.size() - 1)
Sau đây là cách nó được thực hiện. elementData
thực hiện tra cứu trên mảng sao lưu (vì vậy nó có thể cắt nó ra khỏi mảng), phải là thời gian không đổi (vì JVM biết kích thước của tham chiếu đối tượng và số mục nhập có thể tính toán bù trừ) và numMoved
là 0
cho trường hợp này:
public E remove(int index) {
rangeCheck(index); // throws an exception if out of bounds
modCount++; // each time a structural change happens
// used for ConcurrentModificationExceptions
E oldValue = elementData(index);
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null; // Let gc do its work
return oldValue;
}
Nguồn
2013-06-07 15:29:31
Ngoài ra còn có 'remove (int)' ... –
'list.remove (list.size() - 1) '!!! – NINCOMPOOP
Một ngăn xếp có thể là giải pháp tốt hơn ở đây không? –