Skip to content

Instantly share code, notes, and snippets.

@leonardotrp
Created March 14, 2019 01:28
Show Gist options
  • Select an option

  • Save leonardotrp/4e80a10a51bea270c124be02a8f9dfb9 to your computer and use it in GitHub Desktop.

Select an option

Save leonardotrp/4e80a10a51bea270c124be02a8f9dfb9 to your computer and use it in GitHub Desktop.
A doubt about the java keyword modifier 'final'
int bestIndex = 0;
IntStream.range(0, population.size()).forEach(idxIndividual -> {
Individual individual = population.get(idxIndividual);
double x = 0, y = 0;
for (int idxId = 0; idxId < individualSize; idxId++) {
x += eigenvectors.getEntry(idxId, 0) * individual.get(idxId);
y += eigenvectors.getEntry(idxId, 1) * individual.get(idxId);
}
projectionsSeries[idxIndividual] = new double[] {x, y};
if (population.getBest() == individual)
bestIndex = idxIndividual; // PROBLEM HERE!!!
});
// 1º error: Local variable bestIndex defined in an enclosing scope must be final or effectively final
// ... put 'final int bestIndex = 0;' (line 1)
// 2º error: The final local variable bestIndex cannot be assigned. It must be blank and not using a compound assignment
// AND NOW?!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment