Così l'attuazione di tale varietà può essere come questo
import java.util.AbstractList;
import java.util.List;
import java.util.RandomAccess;
public class IntArrayList extends AbstractList<Integer>
implements List<Integer> , RandomAccess /* todo , Cloneable, java.io.Serializable */{
private static final int INT_SIZE_MINUS_ONE = 15;
private static final int RIGHT_SHIFT = 4;
private int size;
private int isNull[];
private int data[];
IntArrayList(int size) {
if (size < 0) {
throw new RuntimeException("invalid size");
}
this.size = size;
isNull = new int[(size + INT_SIZE_MINUS_ONE) >>> RIGHT_SHIFT];
data = new int[size];
}
private void rangeCheck(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException();
}
}
public Integer set(int index, Integer element) {
rangeCheck(index);
Integer oldValue = get(index);
if (element == null) {
isNull[index >>> RIGHT_SHIFT] &= ~(1 << (index & INT_SIZE_MINUS_ONE));
} else {
isNull[index >>> RIGHT_SHIFT] |= (1 << (index & INT_SIZE_MINUS_ONE));
data[index] = element;
}
return oldValue;
}
@Override
public Integer get(int index) {
rangeCheck(index);
if ((isNull[index >>> RIGHT_SHIFT] & (1 << (index & INT_SIZE_MINUS_ONE))) == 0) {
return null;
}
return new Integer(data[index]);
}
@Override
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
}
Può essere utilizzato ovunque come List<Integer>
Tale tipo di oggetto può essere utile per la conservazione a lungo termine delle informazioni che raramente utilizzato. Ridurrà la frammentazione dell'heap a un costo di maggiore generazione di rifiuti sull'accesso agli elementi.
@ 01 - totalmente d'accordo sul tag 'premature-optimization' - questa è solo la discussione teorica che tutti amiamo fare a volte. – ripper234