Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#FFCCCC
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
bgColor#ccccff
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
bgColor#ccccff
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);

...