gpt4 book ai didi

java - 组合 n 个列表列表保存顺序(Java)

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:01:04 24 4
gpt4 key购买 nike

我有一个字符串列表列表[每个元素都在括号中],需要获取字符串列表,每个字符串代表每个列表中一个元素的元素组合。需要得到所有的组合

[+9, +4, +a]
[+i*o -k*z +(m+n+f+5)][+i*o +(m+n+f+5) -k*z][+(m+n+f+5) +i*o -k*z]
[+h*i/o +6*l/8]
[+b/c/r +(a*b*x*y+1)][+(a*b*x*y+1)+b/c/r]

需要得到

[+9, +4, +a][+i*o -k*z +(m+n+f+5)][+h*i/o +6*l/8][+b/c/r +(a*b*x*y+1)]
[+9, +4, +a][+i*o -k*z +(m+n+f+5)][+h*i/o +6*l/8][+(a*b*x*y+1)+b/c/r]
[+9, +4, +a][+i*o +(m+n+f+5) -k*z][+h*i/o +6*l/8][+b/c/r +(a*b*x*y+1)]
[+9, +4, +a][+i*o +(m+n+f+5) -k*z][+h*i/o +6*l/8][+(a*b*x*y+1)+b/c/r]
[+9, +4, +a][+(m+n+f+5) +i*o -k*z][+h*i/o +6*l/8][+b/c/r +(a*b*x*y+1)]
[+9, +4, +a][+(m+n+f+5) +i*o -k*z][+h*i/o +6*l/8][+(a*b*x*y+1)+b/c/r]

感谢任何帮助谢谢

最佳答案

与 Guava Sets.cartesianProduct :

import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;

import java.util.List;
import java.util.Set;


public class Foo {
public static void main(String[] args)
{
final Set<String> s1 = ImmutableSet.of("[+9, +4, +a]");
final Set<String> s2 = ImmutableSet.of("[+i*o -k*z +(m+n+f+5)]","[+i*o +(m+n+f+5) -k*z]","[+(m+n+f+5) +i*o -k*z]");
final Set<String> s3 = ImmutableSet.of("[+h*i/o +6*l/8]");
final Set<String> s4 = ImmutableSet.of("[+b/c/r +(a*b*x*y+1)]","[+(a*b*x*y+1)+b/c/r]");
@SuppressWarnings("unchecked")
final Set<List<String>> cartesianProducts = Sets.cartesianProduct(s1, s2, s3, s4);
for (final List<String> cartesianProduct : cartesianProducts) {
System.out.println(Joiner.on("").join(cartesianProduct));
}
}
}

输出:

[+9, +4, +a][+i*o -k*z +(m+n+f+5)][+h*i/o +6*l/8][+b/c/r +(a*b*x*y+1)]
[+9, +4, +a][+i*o +(m+n+f+5) -k*z][+h*i/o +6*l/8][+b/c/r +(a*b*x*y+1)]
[+9, +4, +a][+(m+n+f+5) +i*o -k*z][+h*i/o +6*l/8][+b/c/r +(a*b*x*y+1)]
[+9, +4, +a][+i*o -k*z +(m+n+f+5)][+h*i/o +6*l/8][+(a*b*x*y+1)+b/c/r]
[+9, +4, +a][+i*o +(m+n+f+5) -k*z][+h*i/o +6*l/8][+(a*b*x*y+1)+b/c/r]
[+9, +4, +a][+(m+n+f+5) +i*o -k*z][+h*i/o +6*l/8][+(a*b*x*y+1)+b/c/r]

关于java - 组合 n 个列表列表保存顺序(Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20784777/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com