假设我传入了3个True和5个False,我如何编写getAllCombos方法以返回一个包含以下内容的列表:

  • 一个1SampleForSO的物体, Select 了3个
  • 三个"三选二"组合
  • 三个"三选一"组合
  • 未 Select 的1,SampleForSO个对象

所以我会返回一个List,其中包含:

  • SampleForSO(T,T,T,F,F,F)
  • SampleForSO(T,T,F,F,F,F)
  • SampleForSO(T,F,T,F,F,F)
  • SampleForSO(F,T,T,F,F,F)
  • SampleForSO(T,T,F,F,F,F)
  • SampleForSO(F,T,T,F,F,F)
  • SampleForSO(T,F,T,F,F,F)
  • SampleForSO(F,F)

public class SampleForSO {

private boolean b0;
private boolean b1;
private boolean b2;
private boolean b3;
private boolean b4;
private boolean b5;
private boolean b6;
private boolean b7;

public SampleForSO(boolean b0, boolean b1, boolean b2, boolean b3, boolean b4, boolean b5, boolean b6, boolean b7) {
    this.b0 = b0;
    this.b1 = b1;
    this.b2 = b2;
    this.b3 = b3;
    this.b4 = b4;
    this.b5 = b5;
    this.b6 = b6;
    this.b7 = b7;
}

public boolean equals(Object o) {
    if(o instanceof SampleForSO) {
        SampleForSO outer = (SampleForSO) o;
        return outer.b0 == this.b0 && outer.b1 == this.b1 && outer.b2 == this.b2 && outer.b3 == this.b3 && outer.b4 == this.b4 && outer.b5 == this.b5 && outer.b6 == this.b6 && outer.b7 == this.b7;
    }
    return false;
    
}

public static List<SampleForSO> getAllCombos(boolean b0, boolean b1, boolean b2, boolean b3, boolean b4, boolean b5, boolean b6, boolean b7){
    // How to get all the combos?
    return new ArrayList<SampleForSO>();
}

}


Here's a JUnit test that would show green if that method worked:
public class SampleForSOTest {

    public SampleForSOTest() {
        super();
    }
    
    @Test
    public void testGetAllCombinations() throws Exception {
        
        // 3 true, 5 false, but need this to work for all possibilities
        boolean b0_ = true;
        boolean b1_ = true;
        boolean b2_ = true;
        boolean b3_ = false;
        boolean b4_ = false;
        boolean b5_ = false;
        boolean b6_ = false;
        boolean b7_ = false;

        int numCombosWithAllThree = 1;
        int numCombosWithTwo = 3;
        int numCombosWithOne = 3;
        int numCombosWithZero = 1;

        int expectedSize = numCombosWithAllThree + numCombosWithTwo + numCombosWithOne + numCombosWithZero;
        
        List<SampleForSO> allCombos = SampleForSO.getAllCombos(b0_, b1_, b2_, b3_, b4_, b5_, b6_, b7_);

        assertEquals(expectedSize, allCombos.size());
    }

    

}

我想要的组合是:

  • b0,b1,b2全部为真,其余全部为假
  • b0 b1 true,all other false
  • b1 b2 true,all other false
  • B2b0为真,其他均为假
  • b0 true,all other false
  • B1是真的,其他都是假的
  • B2为真,其他均为假
  • 所有假

推荐答案

以下代码生成所有组合

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {

  public static List<List<Boolean>> generateAllCombinations(List<Boolean> input) {
    var combinations = new ArrayList<List<Boolean>>();
    if (input.isEmpty()) {
      return combinations;
    }

    // add combination seed
    combinations.add(new ArrayList<>());
    for (var inputIndexValue : input) {
      var nextCombinations= new ArrayList<List<Boolean>>();

      for (var indexCombination : combinations) {
          
        if (inputIndexValue) {
           // true is only valid if inputIndexValue is true
          var newCombination = new ArrayList<>(indexCombination);
          newCombination.add(true);
          nextCombinations.add(newCombination);
        }
        
        // false is always a valid value
        var newCombination = new ArrayList<>(indexCombination);
        newCombination.add(false);
        nextCombinations.add(newCombination);
      }

      combinations = nextCombinations;
    }

    return combinations;
  }

  public static void main(String[] args) {
    var input = List.of(true, true, true, false, false, false, false, false);
    var allCombinations = generateAllCombinations(input);
    for (var combination : allCombinations) {
      System.out.println(combination);
    }
  }
}

Java相关问答推荐

是否可以从@ TrustMapping中删除特定方法的基路径?

找到允许的最大底片

Quarkus keycloat配置不工作.quarkus. keycloak. policy—enforcer. enable = true在. yaml表示中不工作

Java List with all combinations of 8 booleans

@ IdClass with @ Inheritance(策略= InheritanceType. SINGLE_TABLE)

springboot start loge change

Java Swing:初始化身份验证类后未检测到ATM_Interface键事件

对于几乎不涉及逻辑的请求,您是否应该使用命令模式?

如何获得执行人?

如何创建一个2d自上而下的移动系统,其中移动,同时持有两个关键是可能的处理?

内存中的H2修剪尾随空格

在Java中,如何按一个属性升序,然后按另一个属性降序对对象列表进行排序?

无法使用Java&;TestContainers获取AWS SQS队列的属性

Jenv-相同的Java版本,但带有前缀

为什么我的回收视图会显示重复的列表?

在macOS上读取文件会导致FileNotFound,即使文件存在(并且具有权限)

删除打印语句会影响功能...腐败在起作用?

嘲笑黄瓜中的对象

Vaadin Flow:设置密码显示按钮属性

为什么 Random() 的行为不符合预期?