Original code:
String currentState = new String[answer.length()];
for(String x : currentState)
{
x = "_";
}
Rewritten code:
String currentState = new String[answer.length()];
for(int i = 0; i < currentState.length; i++)
{
String x;
x = currentState[i];
x = "_";
}
How I would write the code:
String currentState = new String[answer.length()];
for(final String x : currentState)
{
x = "_"; // compiler error
}
Rewritten code with the error:
String currentState = new String[answer.length()];
for(int i = 0; i < currentState.length; i++)
{
final String x;
x = currentState[i];
x = "_"; // compiler error
}
Making the variables final highlights when you do things like this (it is a common beginner mistake). Try to make all of your variables final (instance, class, arguments, exceptions in catch. etc...) - only make them non-final if you really have to change them. You should find that 90%-95% of your variables are final (beginners will wind up with 20%-50% when they start doing this).