this sounds like a quite stupid question, but I have the problem, that a actions, depending on a boolean value, which is a parameter should be executed for every element in a data structure like an array. Now i am asking for this value at every pass in the loop, which is very inefficient, because this value doesn't change in that method and I need a better solution. I actually tried to combine to methods, because I was teached that a code is better, when it has few lines. But I think that also the performance is important (i measured the time that was needed to execute all methods and the combined methode took more than 100 times longer).
Combined:
public int[][] copyGrid(boolean g1ToT1) {
int[][] g = new int[9][9];
int[][] g1 = s.getGrid();
for(int i=0; i<t.length; i++) {
for(int j=0; j<t.length; j++) {
if(g1ToT1) t[i][j].setText(Integer.toString(g1[i][j]));
else {
try {
g[i][j] = Integer.parseInt(t[i][j].getText());
} catch(Exception e) {}
}
}
}
return g;
}
Method1:
public int[][] gridToSudoku() {
int[][] g = new int[9][9];
for(int i=0; i<t.length; i++) {
for(int j=0; j<t.length; j++) {
try {
g[i][j] = Integer.parseInt(t[i][j].getText());
} catch(Exception e) {}
}
}
return g;
}
Method2:
public void sudokuToGrid(int[][] g) {
for(int i=0; i<9; i++) {
for(int j=0; j<9; j++) {
t[i][j].setText(g[i][j] == 0 ? "" : Integer.toString(g[i][j]));
}
}
}
Aucun commentaire:
Enregistrer un commentaire