Use ArrayList instead of array. This won't take much extra memory, but will simplify code.