Java コードで、内部クラス内にブール値フラグを設定する必要がある場合がよくあります。内部クラスは外部からの最終変数でしか機能しないため、プリミティブブール型を使用することはできません。そのため、次のようなパターンを使用します。
// class from gnu.trove is not of big importance, just to have an example
private final TIntIntHashMap team = new TIntIntHashMap();
// ....... code ............
final boolean[] flag = new boolean[]{false};
team.forEachValue(new TIntProcedure() {
@Override
public boolean execute(int score) {
if(score >= VICTORY_SCORE) {
flag[0] = true;
}
return true; // to continue iteration over hash map values
}
});
// ....... code ..............
非最終変数の代わりに最終配列のパターンはうまく機能しますが、私には十分に美しく見えません。誰かがJavaでより良いパターンを知っていますか?