Removing last object of ArrayList in Java
-- the
list.remove(list.size() - 1) - this is O(1), as numMoved is 0.
JDK should rename remove(int index) to meaningful name such as removeIndex. - and deprecate the old one.
final ArrayList<Integer> a1 = Lists.newArrayList(1, 2, 3);
// a1.remove(a1.get(0)); // remove object
a1.remove((Integer) 1); // remove object
a1.remove(1); // remove index
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;
}
-- the
list.remove(list.size() - 1) - this is O(1), as numMoved is 0.
JDK should rename remove(int index) to meaningful name such as removeIndex. - and deprecate the old one.
final ArrayList<Integer> a1 = Lists.newArrayList(1, 2, 3);
// a1.remove(a1.get(0)); // remove object
a1.remove((Integer) 1); // remove object
a1.remove(1); // remove index
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;
}
from Public RSS-Feed of Jeffery yuan. Created with the PIXELMECHANICS 'GPlusRSS-Webtool' at http://gplusrss.com http://ift.tt/1lklk1L
via LifeLong Community