code: private void calculateNearestNeighbors(int nn) {
          int n = this.dimension;
          if ((nn > 0) && (nn < n)) {
              nearestNeighbors = new int[n][nn];
  
              Neighbor[] neighbors = new Neighbor[n];
              for (int i = 0; i < n; i++) neighbors[i] = new Neighbor();
  
              for (int i = 0; i < n; i++) {
                  for (int j = 0; j < n; j++) {
                      neighbors[j].id = j;
                      neighbors[j].distance = this.distances[i][j];
                  }
                  neighbors[i].distance = Integer.MAX_VALUE;
                  Arrays.sort(neighbors);
                  for (int j = 0; j < nn; j++) nearestNeighbors[i][j] = neighbors[j].id;
              }
          }
       }
docs: Calculate nn nearest neighbors for each city (used by various speed up techniques)
  
  @param nn