I have this code, where the conditions are very similar and the methods being called are the same. I was wondering if there's a way to make this look better, or at least make it smaller and thus easier to read.
public void open(int i, int j){
if (i > n - 1 || i < 0 || j > n - 1 || j < 0) {
throw new IndexOutOfBoundsException
("Index out of bounds.");
}
grid[i][j] = true;
if (i - 1 >= 0 && grid[i - 1][j]) {
unionFind.union(location(i - 1, j), location(i, j));
unionFind2.union(location(i - 1, j), location(i, j));
}
if (i + 1 < n && grid[i + 1][j]) {
unionFind.union(location(i + 1, j), location(i, j));
unionFind2.union(location(i + 1, j), location(i, j));
}
if (j - 1 >= 0 && grid[i][j - 1]) {
unionFind.union(location(i, j - 1), location(i, j));
unionFind2.union(location(i, j - 1), location(i, j));
}
if (j + 1 < n && grid[i][j + 1]) {
unionFind.union(location(i, j + 1), location(i, j));
unionFind2.union(location(i, j + 1), location(i, j));
}
numberOpen++;
}
Aucun commentaire:
Enregistrer un commentaire