Reputation: 43
I have an array of elements (even number) and I want to get all possible combinations of them in couples.
If the array is
String[] test = new String[4];
String[] test= {"e1","e2","e3","e4"};
The output should be:
Combination1 e1-e2 , e3-e4
Combination2 e1-e3 , e2-e4
Combination3 e1-e4 , e2-e3
Combination4 e4-e1 , e3-e2
Combination5 e3-e1 , e4-e2
Combination6 e2-e1 , e4-e3
Upvotes: 3
Views: 2334
Reputation: 13483
String[] test = {"e1","e2","e3","e4"};
for (int i = 0; i < test.length; i++) {
for (int j = i + 1; j < test.length; j++) {
System.out.print(test[i] + " - " + test[j]);
boolean foundExtra = false;
for (int k = 0; k < test.length && !foundExtra; k++)
{
if (k != j && k != i)
{
for (int l = 0; l < test.length; l++)
{
if (l != k && l != j && l != i)
{
System.out.println(" , " + test[k] + " - " + test[l]);
foundExtra = true;
break;
}
}
}
}
}
}
Will give the output:
e1 - e2 , e3 - e4
e1 - e3 , e2 - e4
e1 - e4 , e2 - e3
e2 - e3 , e1 - e4
e2 - e4 , e1 - e3
e3 - e4 , e1 - e2
This is not the output you put in your question, but I believe this is the output you want, judging from your sports teams comment.
Don't be scared of the loops - I did this in one try because I thought threw it easily.
What I was thinking: Find all of the combinations, like in my previous answer. That's what I started with. Next thing I did (the last 2 of 4 loops - k
and l
), was check for the other teams that were left.
So cycle through all of the elements, check if i
and j
were not already used, then that was k
. Then, cycle through all of the elements again, check if i
, j
, and k
were not already used, then that was l
.
Upvotes: 1
Reputation: 1
Try this one:
String[] test= {"e1","e2","e3","e4"};
int count=1;
for(int i=0; i<test.length/2; i++)
for(int k=0, j=0; k<test.length; k++,j++) {
if(i==k) k++; if(j==i+test.length/2) j++;
System.out.print("Permutatation "+count+": ");
System.out.println(test[i]+"-"+test[k]+", "+test[i+test.length/2]+"-"+test[j]);
count++;
}
Output:
Permutatation 1: e1-e2, e3-e1
Permutatation 2: e1-e3, e3-e2
Permutatation 3: e1-e4, e3-e4
Permutatation 4: e2-e1, e4-e1
Permutatation 5: e2-e3, e4-e2
Permutatation 6: e2-e4, e4-e3
Upvotes: 0
Reputation: 167972
This should work for an array of any even number of elements (and is generic so should work for other things than strings as well):
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.LinkedList;
public class Q2112634 {
public static class Pair<X> {
final X first;
final X second;
public Pair( final X one, final X two){
first = one;
second = two;
}
public String toString(){
return first.toString() + "-" + second.toString();
}
public String toReverseString(){
return second.toString() + "-" + first.toString();
}
}
@SuppressWarnings("unchecked")
public static <X> void getCombinations( final ArrayList<Pair<X>[]> combinations, final LinkedList<Pair<X>> pairs, final LinkedList<X> list ) {
// System.out.println(list.size());
final X first = list.removeFirst();
for ( int i = 0; i < list.size(); ++i ) {
final X second = list.remove( i );
final Pair<X> p = new Pair<X>( first, second );
pairs.addLast( p );
if ( list.size() == 0 )
{
combinations.add( pairs.toArray( (Pair[]) Array.newInstance( p.getClass(), pairs.size() ) ) );
}
else
{
getCombinations( combinations, pairs, list );
}
list.add( i, second );
pairs.removeLast();
}
list.addFirst( first );
}
static <E> String arrayToString( E[] arr ) {
if ( arr.length == 0 )
return "";
final StringBuffer str = new StringBuffer();
str.append( arr[0].toString() );
for ( int i = 1; i < arr.length; ++i )
{
str.append( ',' );
str.append( arr[i].toString() );
}
return str.toString();
}
public static void main(String[] args) {
String[] test = { "e1", "e2", "e3", "e4" };
int num_combinations = 1;
for ( int i = test.length - 1; i > 1; i = i - 2 )
num_combinations *= i;
final ArrayList<Pair<String>[]> combinations = new ArrayList<Pair<String>[]>( num_combinations );
final LinkedList<String> strings = new LinkedList<String>();
for ( String s : test )
strings.add( s );
getCombinations( combinations, new LinkedList<Pair<String>>(), strings );
System.out.println( "-----Combinations-----" );
int i = 1;
for ( Pair<String>[] combination: combinations ){
System.out.println( "Combination " + (i++) + " " + arrayToString( combination ) );
}
System.out.println( "-----Permutations-----" );
i = 1;
for ( Pair<String>[] combination: combinations ){
for ( int j = 0; j < Math.pow( 2, combination.length ); ++j )
{
System.out.print( "Permutation " + (i++) + " " );
for ( int k = 0; k < combination.length; ++k )
{
if ( k > 0 )
System.out.print(',');
if ( (j & ( (int) Math.pow( 2, k ) ) ) == 0 )
System.out.print( combination[k].toString() );
else
System.out.print( combination[k].toReverseString() );
}
System.out.println();
}
}
}
}
Output
-----Combinations-----
Combination 1 e1-e2,e3-e4
Combination 2 e1-e3,e2-e4
Combination 3 e1-e4,e2-e3
-----Permutations-----
Permutation 1 e1-e2,e3-e4
Permutation 2 e2-e1,e3-e4
Permutation 3 e1-e2,e4-e3
Permutation 4 e2-e1,e4-e3
Permutation 5 e1-e3,e2-e4
Permutation 6 e3-e1,e2-e4
Permutation 7 e1-e3,e4-e2
Permutation 8 e3-e1,e4-e2
Permutation 9 e1-e4,e2-e3
Permutation 10 e4-e1,e2-e3
Permutation 11 e1-e4,e3-e2
Permutation 12 e4-e1,e3-e2
Upvotes: 0
Reputation: 41097
Write two for
loops :
for (int i =0; i < test.length; i++) {
for (int j = i + 1; j < test.length; j++) {
// code to print a[i] - a[j]
}
}
Upvotes: 1