简体   繁体   English

java:循环两个布尔值(假,真)

[英]java: looping on the two boolean values (false, true)

This is a stylistic question.这是一个文体问题。 I want to loop twice with a variable on which is set to false, then to true.我要循环两次,一个变量on被设置为false,那么为true。 Which of these is clearer:以下哪个更清楚:

A)一种)

for (final boolean on : new boolean[] { false, true} )
{
   doStuffBasedOnABooleanFlag(on);
}

B)乙)

for (int i = 0; i < 2; ++i)
{
   final boolean on = (i == 1);
   doStuffBasedOnABooleanFlag(on);
}

C) something else C) 别的东西


edit: Murphy's law of unintended interpretations comes into play... the use case I have originally looked something like this instead of doStuffBasedOnABooleanFlag:编辑:墨菲的意外解释定律开始发挥作用......我最初看起来像这样的用例而不是 doStuffBasedOnABooleanFlag:

for (final boolean on : new boolean[] { false, true} )
{
   JButton button = on ? onButton : offButton;
   button.addActionListener(new ActionListener() {
      @Override public void actionPerformed(ActionEvent event) {
      doStuffLaterBasedOnABooleanFlag(on);
      }
   }
}

But I think I like Brendan's answer, I'll just refactor the loop contents into a separate method:但我想我喜欢 Brendan 的回答,我只是将循环内容重构为一个单独的方法:

doStuffBasedOnABooleanFlag(false);
doStuffBasedOnABooleanFlag(true);

   ...

private void doStuffBasedOnABooleanFlag(final boolean on)
{
   JButton button = on ? onButton : offButton;
   button.addActionListener(new ActionListener() {
      @Override public void actionPerformed(ActionEvent event) {
      doStuffLaterBasedOnABooleanFlag(on);
      }
   }
}

Since it's two lines, I'd just skip the loop and do:由于它是两行,我只是跳过循环并执行以下操作:

doStuffBasedOnABooleanFlag(false);
doStuffBasedOnABooleanFlag(true);

Less code, more obvious, more efficient.更少的代码,更明显,更高效。

Another option would be to avoid the boolean and use an enum:另一种选择是避免使用布尔值并使用枚举:

enum Mode { APPEND, REPLACE } // or whatever your boolean indicated

You could then either iterate:然后你可以迭代:

for(Mode m : Mode.values()) doStuff(m);

Or do the calls directly:或者直接拨打电话:

doStuff(Mode.APPEND);
doStuff(Mode.REPLACE);

The advantage of this would be that the API indicates more clearly what's happening.这样做的好处是 API 可以更清楚地指示正在发生的事情。

If you really want to use a loop, I would go with (a).如果你真的想使用循环,我会选择(a)。 While it's novel, it's also clear and efficient.虽然它很新颖,但它也很清晰和高效。 I might move the boolean array to a private static, to avoid recreating the array every time.我可能会将布尔数组移动到私有静态,以避免每次都重新创建数组。

But I like Brendan's answer better.但我更喜欢布伦丹的回答。

It's not just the loop, I'm also really uncomfortable with the use of booleans in this way.这不仅仅是循环,我也对以这种方式使用布尔值感到非常不舒服。

What about something like:怎么样:

  ActionListener myListener = new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent event) {
      doStuffLaterBasedOnABooleanFlag(event.getSource() == onButton);
    }
  };
  onButton.addActionListener(myListener);
  offButton.addActionListener(myListener);

That still leaves the boolean within the listener, but without knowing what the doStuffLater method does that's as far as we can go.这仍然在侦听器中留下了布尔值,但不知道 doStuffLater 方法做了什么,我们可以做到这一点。

It can be done directly within a for loop, without creating a new array.它可以直接在 for 循环中完成,而无需创建new数组。

for (boolean on=false, done=false; !done; done=on, on=true) {
    System.out.println("on="+on+", done="+done);
}

Output:输出:

on=false, done=false
on=true, done=false

This isn't the most clear way, so I wouldn't use this approach unless it were in some kind of inner loop where it would be executed a large number of times.这不是最清楚的方法,所以我不会使用这种方法,除非它在某种内部循环中会被执行很多次。

More fancy loop:更花哨的循环:

IntStream.range(0, 2).forEach(x -> {
    boolean flag = x == 0;
    doStuffBasedOnABooleanFlag(flag);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM