How can two Java list elements access each other?
<p>The root of the problem for me is that Java does not allow references.</p>

<p>The problem can be summarized succinctly.  Imagine you have a List of Blob objects:</p>

<pre><code>class Blob {
    public int xpos;
    public int ypos;
    public int mass;
    public boolean dead;
    private List&lt;Object&gt; giganticData;
    public void blobMerge(Blob aBlob) {
        . . .
        if (. . .) {
            this.dead = true;
        } else {
            aBlob.dead = true;
        }
    }
}
</code></pre>

<p>If two blobs are close enough, they should be merged, meaning one of the two blobs being compared should take on the attributes of the other (in this case, adding the mass and merging the giganticData sets) and the other should be marked for deletion from the list.</p>

<p>Setting aside the problem of how to optimally identify adjacent blobs, a stackoverflow question in its own right, how do you keep the blobMerge() logic in the Blob class?  In C or C++ this would be straightforward, as you could just pass one Blob a pointer to the other and the "host" could do anything it likes to the "guest".</p>

<p>However, blobMerge() as implemented above in Java will operate on a copy of the "guest" Blob, which has two problems.  1) There is no need to incur the heavy cost of copying giganticData, and 2) the original copy of the "guest" Blob will remain unaffected in the containing list.</p>

<p>I can only see two ways to do this:</p>

<p>1) Pass the copies in, doing everything twice.  In other words, Blob A hosts Blob B and Blob B hosts Blob A.  You end up with the right answer, but have done way more work than necessary.</p>

<p>2) Put the blobMerge() logic in the Class that contains the containing List.  However, this approach scales very poorly when you start subclassing Blob (BlueBlob, RedBlob, GreenBlob, etc.) such that the merge logic is different for every permutation.  You end up with most of the subclass-specific code in the generic container that holds the list.</p>

<p>I've seen something about adding References to Java with a library, but the idea that you have to use a library to use a Reference put me off that idea.</p>
