private void ensureDistances(){
  if (getDistanceEnd() > distances.length) {
    int newLength=distances.length * 4;
    double[] newDistances=new double[newLength];
    System.arraycopy(distances,0,newDistances,0,distances.length);
    Arrays.fill(newDistances,distances.length,newLength,-1);
    distances=newDistances;
  }
}

private void ensureDistances ( ) { if ( getDistanceEnd ( ) > distances . length ) { int newLength = distances . length * 4 ; double [ ] newDistances = new double [ newLength ] ; System . arraycopy ( distances , 0 , newDistances , 0 , distances . length ) ; Arrays . fill ( newDistances , distances . length , newLength , - 1 ) ; distances = newDistances ; } }
makes sure there is enough space in the distances array
