我有这堂课
public class Operation {
private double value;
private boolean inc;
public Operation(double value, boolean inc) {
this.value = value;
this.inc = inc;
}
public double getValue() {
return value;
}
public void setValue(double value) {
this.value = value;
}
public boolean isInc() {
return inc;
}
public void setInc(boolean inc) {
this.inc = inc;
}
@Override
public String toString() {
return "Operation{" + "value=" + value + ", inc=" + inc + '}';
}
}
现在我有了这个列表
public class TestOperation {
public static void main(String[] args) {
List<Operation> listOperation1 = new ArrayList<>();
listOperation1.add(new Operation(1.3, true));
listOperation1.add(new Operation(2.7, true));
listOperation1.add(new Operation(0.9, false));
listOperation1.add(new Operation(0.8, false));
//Generate posible Rotation listOperation1
//Operation(1.3, true), Operation(2.7, true), Operation(0.9, false), Operation(0.8, false)
//Operation(1.3, true), Operation(2.7, true), Operation(0.8, false), Operation(0.9, false)
//Operation(2.7, true), Operation(1.3, true), Operation(0.9, false), Operation(0.8, false)
//Operation(2.7, true), Operation(1.3, true), Operation(0.8, false), Operation(0.9, false)
List<Operation> listOperation2 = new ArrayList<>();
listOperation2.add(new Operation(1.5, true));
listOperation2.add(new Operation(2.9, true));
listOperation2.add(new Operation(4.6, true));
//Generate posible Rotation listOperation2
//Operation(1.5, true), Operation(2.9, true), Operation(4.6, true)
//Operation(1.5, true), Operation(4.6, true), Operation(2.9, true)
//Operation(2.9, true), Operation(4.6, true), Operation(1.5, true)
//Operation(2.9, true), Operation(1.5, true), Operation(4.6, true)
//Operation(4.6, true), Operation(2.9, true), Operation(1.5, true)
//Operation(4.6, true), Operation(1.5, true), Operation(2.9, true)
}
}
仅在inc = true
和inc = false
之间发生旋转,不能混合!
项目inc = true
的数量可以不同inc = false
。甚至可能不存在。
列表中所有提交的项目都必须处理,不能丢失,但不能重复(像对象,不像值)。
如何生成此轮播?
答案 0 :(得分:0)
public class JavaApplication7 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
List<Operation> listOperation1 = new ArrayList<>();
listOperation1.add(new Operation(1.3, true));
listOperation1.add(new Operation(2.7, true));
listOperation1.add(new Operation(0.9, false));
listOperation1.add(new Operation(0.8, false));
List listOperationTrue = listOperation1.stream()
.filter(operation -> operation.isInc())
.collect(Collectors.toList());
List listOperationFalse = listOperation1.stream()
.filter(operation -> !operation.isInc())
.collect(Collectors.toList());
List<List<Operation>> permutationTrue = new ArrayList<>();
permuteList(listOperationTrue, 0, permutationTrue);
System.out.println("\npermutationTrue");
permutationTrue.stream().forEach(System.out::print);
System.out.println();
List<List<Operation>> permutationFalse = new ArrayList<>();
permuteList(listOperationFalse, 0, permutationFalse);
System.out.println("\npermutationFalse");
permutationFalse.stream().forEach(System.out::print);
System.out.println("\n\npermutationTrue -> permutationFalse");
permutationTrue.stream().forEachOrdered(pTrue -> {
permutationFalse.stream().forEachOrdered(pFalse -> {
pTrue.stream().forEach(System.out::print);
System.out.print(" -> ");
pFalse.stream().forEach(System.out::print);
System.out.println();
});
});
}
private static void permuteList(List<Operation> listOperation, int pos, List<List<Operation>> out) {
if (pos == listOperation.size() - 1) {
out.add(listOperation);
} else {
for (int i = pos; i < listOperation.size(); i++) {
listOperation = swap(listOperation, pos, i);
permuteList(listOperation, pos + 1, out);
listOperation = swap(listOperation, pos, i);
}
}
}
public static List<Operation> swap(List<Operation> listOperation, int i, int j) {
List<Operation> outSwap = new ArrayList<>(listOperation);
outSwap.set(i, listOperation.get(j));
outSwap.set(j, listOperation.get(i));
return outSwap;
}
}
输出:
permutationTrue
[Operation{value=1.3, inc=true}, Operation{value=2.7, inc=true}][Operation{value=2.7, inc=true}, Operation{value=1.3, inc=true}]
permutationFalse
[Operation{value=0.9, inc=false}, Operation{value=0.8, inc=false}][Operation{value=0.8, inc=false}, Operation{value=0.9, inc=false}]
permutationTrue -> permutationFalse
Operation{value=1.3, inc=true}Operation{value=2.7, inc=true} -> Operation{value=0.9, inc=false}Operation{value=0.8, inc=false}
Operation{value=1.3, inc=true}Operation{value=2.7, inc=true} -> Operation{value=0.8, inc=false}Operation{value=0.9, inc=false}
Operation{value=2.7, inc=true}Operation{value=1.3, inc=true} -> Operation{value=0.9, inc=false}Operation{value=0.8, inc=false}
Operation{value=2.7, inc=true}Operation{value=1.3, inc=true} -> Operation{value=0.8, inc=false}Operation{value=0.9, inc=false}
BUILD SUCCESSFUL (total time: 0 seconds)