排列 组合 java,找出所有组合和排列-Java

I am all new here and also a beginner. I am working on a personal project (developing an app for personal use). I am a bit stuck and yes, I did search around but couldn't find much.

My aim is to find all possible combinations. Let's say I have a finite number of lists defined like:

List AList contains {"a1","a2","a3","a4"}

List BList contains {"b1","b2","b3"}

List CList contains {"c1","c2","c3","c4","c5"}

List DList contains {"d1","d2"}

I would like to find all combinations with:

1) the combination a1 a2 vein the same as a2 a1 and so on

2) the number of elements per result set not being fixed: a combination possibility is per example merging all lists into one.. or each element in a list of its own or merging two elements in a list and the rest in another.. and so on..

I know it has to be a recursive function.. but that how far i am right now.. Any help would be appreciated. Thanks

解决方案

i did a similar thing for myself, this however would give you permutations of a1 a2 etc and not combinations, meaning the order does not matter.

public void permutation(String prefix, String str) {

int n = str.length();

if (n==0){}

else {

for (int i = 0; i < n; i++)

permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i+1, n));

}

}

With this logic, you would have to turn your Lists into one long string and then send this method your string

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值