...
| Code Block | ||
|---|---|---|
| ||
class DataElement {
private boolean dead = false;
// Other fields
public boolean isDead() { return dead; }
public void killMe() { dead = true; }
}
// Elsewhere
ListList<DataElement> longLivedList = new ArrayList<DataElement>();
// Processing that renders an element irrelevant
// Kill the element that is now irrelevant
longLivedList.get(someIndex).killMe();
|
...
| Code Block | ||
|---|---|---|
| ||
class DataElement {
// Dead flag removed
// Other fields
}
// Elsewhere
ListList<DataElement> longLivedList = new ArrayList<DataElement>();
// Processing that renders an element irrelevant
// Set the reference to the irrelevant DataElement to null
longLivedList.set(someIndex, null);
|
...
| Code Block | ||
|---|---|---|
| ||
class DataElement {
public static final DataElement NULL = createSentinel();
// Dead flag removed
// Other fields
private static final DataElement createSentinel() {
// Allocate a sentinel object, setting all its fields
// to carefully chosen "do nothing" values
}
}
// Elsewhere
ListList<DataElement> longLivedList = new ArrayList<DataElement>();
// Processing that renders an element irrelevant
// Set the reference to the irrelevant DataElement to
// the NULL object
longLivedList.set(someIndex, DataElement.NULL);
|
...