By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,969 Members | 2,422 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,969 IT Pros & Developers. It's quick & easy.

How I can get all combinations(without repetition)of 1-2-3-n Element?

P: 4
Body: I want to create a method that that takes an Integer "k" and an "ArrayList of String" with a size n, and after, returns all combinations.

this is an example for n=3 (n is the size of the array) elements:
//ArrayList <String>list==> {ax,by,cz}
String solution="";
for(int k=1;k<=n;k++){
solution+=solution+function(k,list)//function(k,list) is the problem
String[] sol=solution.split();

//Solution expected:
//sol[3]=axby "or" byax
//sol[4]=bycz "or" czby
//sol[5]=axcz "or" czax
//sol[6]=axbycz"or" any other combination of 3 elements for ax-by-cz
Nov 6 '18 #1
Share this Question
Share on Google+
1 Reply

P: 1
U can make a recursive metod that takes an "Strings[] list" and a "integer k", that returns an "String[] answers".

And after, you only need make a while cicle to add new solutions, this is an example:

String[] list= new String[n];//u have to add the elements needed;
int k=1;
int aux;
String[] combs =recursive(list,k);//here you get new combinations

String[] solution//and here you need make a method using "aux" and "sol" for add new combinations

Nov 6 '18 #2

Post your reply

Sign in to post your reply or Sign up for a free account.