423,688 Members | 1,894 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,688 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[0]=ax
//sol[1]=by
//sol[2]=cz
//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
2 Weeks Ago #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;
While(k!=n){
String[] combs =recursive(list,k);//here you get new combinations
aux=combs.length;

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

k++;
}
2 Weeks Ago #2

Post your reply

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