Java: Problems in Changing for each loop to normal for loop in method -
i have following method , wanted change each loops normal loops. tried this
for (int = 0; < above.length(); i++) { char x = above.tochararray(); if (x == 'x') { counter++; } } but know that's wrong. so, what's right way change these each loops normal loops?
public static int neighbourconditions(string above, string same, string below){ int counter = 0; if(above != null){ for(char x : above.tochararray()){ if(x == 'x'){ counter++; } } } (char x : same.tochararray()){ if (x == 'x'){ counter++; } } if (below != null){ for(char x : below.tochararray()){ if (x == 'x'){ counter++; } } } return counter; }
just use basic for loop bounds governed length of string, same size of corresponding character array, e.g. first loop:
for (char x : above.tochararray()) { if (x == 'x') { counter++; } } would become this:
for (int x=0; x < above.length(); ++x) { if (above.charat(x) == 'x') { counter++; } }
Comments
Post a Comment