import java.awt.*; class Location { // Assume this class was already implemented // ... } class Marker { public boolean isCloseTo (Location location) { // Assume this method was already implemented // ... } } class GooglePlanet { Image _satelliteImage; Marker[] _markers; // Return an array of Marker objects that are "close to" the specified locatio, // where "close to" is defined by the Marker.isCloseTo() method. public Marker[] findLocalMarkers (Location location) { Marker[] localMarkers = new Marker[128]; // initialize to some small size int idx = 0; for (Marker marker : _markers) { if (marker.isCloseTo(location)) { if (idx == localMarkers.length) { // Array is full // Allocate a new array twice as big as the last one Marker[] newLocalMarkers = new Marker[2*localMarkers.length]; // Copy the old array into the new array for (int i = 0; i < localMarkers.length; i++) { newLocalMarkers[i] = localMarkers[i]; } // Now, make localMarkers point to that *new* array -- the "old" // version of localMarkers will be swept away by the garbage collector. localMarkers = newLocalMarkers; } // Now, we know we definitely have enough room to store one more marker localMarkers[idx] = marker; idx++; } } // We still have to "trim down" the localMarkers array to the exact number of Marker objects. // that we actually added -- this is recorded in idx. Let's allocate one more Marker[] // to store the correct number of objects. Marker[] newLocalMarkers = new Marker[idx]; for (int i = 0; i < idx; i++) { newLocalMarkers[i] = localMarkers[i]; } localMarkers = newLocalMarkers; return localMarkers; } }