Hi I've been having this problem for a while. It seems that every time I try to use the add method on an ArrayList to insert an object into the list I get an error stating "out of memory...heap overrun". I would understand if these lists were large and the objects contained within then were complex but neither is the case. Here is the code snippet that causes the problem. Thanks in advance for any insight!
ResultObject ro = new ResultObject(str, distance);
if(res_.isEmpty()) res_.add //Places first object in the arrayList
for(int i =0; i<res_.size();i++) //walks through the arrayList
if(distance < res_.get(i).getDistance()) //This statement attempts to order the arrayList at time of entry by comparing the current result to the value in the arrayList
res_.add(i,ro) //This is the statement that causes the memory overrun!
added = true;
if(!added) //adds the object to the end of the list if it was not inserted above.
added = false;
Use TreeSet instead of ArrayList.
TreeSet automatically sorts items and checks if new item is already in the set,
and is more efficient than manually ordering of ArrayList.
With TreeSet your above code will simplify to only two lines:
The only thing you must do to use TreeSet is to implement equals() and compareTo() methods in your ResultObject class.