Issue
How to generate combinations from char array? here is my code https://anotepad.com/notes/icjsc5ct I have an array with 5 characters: a, b, c, d, e And I want to generate combinations of 3 characters
- [a, b, c];
- [a, b, d];
- [b, c, d];
- [b, c, e];
- [c, d, e]
My code just can generate 2 combinations of 3 character:
- [a, b, c];
- [a, b, d];
I don't know how to increase index of Array[0] and Array[1];
public ArrayList< char[] > generate02( int r ) {
ArrayList< char[] > combinationsList = new ArrayList<>();
char[] data = new char[ r ];
// initialize with lowest lexicographic combination
for ( int i = 0; i < r; i++ ) {
data[ i ] = CharArray01[ i ];
}
PrintData( CharArray01 );
int n = CharArray01.length;
while ( IndexInt( data[ r - 1 ] ) < n - 1 ) {
int t01 = r - 1;
System.out.println( " IndexInt( data[ r - 1 ] ) < n " );
System.out.println( " data[ r - 1 ] ) = " + data[ t01 ]
+ "; IndexInt( data[ r - 1 ] ) = "
+ IndexInt( data[ r - 1 ] )
+ "; n = " + n );
combinationsList.add( data.clone() );
// generate next combination in lexicographic order
int t02 = n - r + t01;
while ( t01 != 0 && IndexInt( data[ t01 ] ) == t02 ) {
t01--;
}
int k1 = IndexInt( data[ r - 1 ] );
int k2 = k1 + 1;
data[ r - 1 ] = IndexChar( k2 );
System.out.println( " data[ r - 1 ] ) = " + data[ t01 ]
+ "; IndexInt( data[ r - 1 ] ) = "
+ IndexInt( data[ r - 1 ] ) );
System.out.println( "t01 = " + t01 + "; n = " + n );
int i = 0;
for ( i = t01 + 1; i < r; i++ ) {
int index02 = IndexInt( data[ i - 1 ] );
int index03 = index02 + 1;
data[ i ] = data[ index03 ];
}
}
return combinationsList;
}
```
Function PrintData convert a char array into String and Display it for my debug
public void PrintData( char[] CharArray02 ) { int length = CharArray02.length; String string01 = ""; for ( int i = 0; i < length; i++ ) { string01 = string01 + CharArray02[ i ] + " "; } System.out.println( "PrintData: String01 = " + string01 ); }
Function IndexInt return position of char (start from 0) in sample string "abcde"
public int IndexInt( char char01 ) { int result = 0; String string01 = "abcde"; int length = string01.length(); for ( int i = 0; i < length; i++ ) { if ( char01 == (char) string01.charAt( i ) ) { return i; } } return result; }
Function IndexChar return char at int position in sample string "abcde"
public char IndexChar( int index01 ) { char result = 'a'; String string01 = "abcde"; result = string01.charAt( index01 ); return result; }
Function
public ArrayList<char[]> generate02(int r)
is the copy and modified code from algorithm integer combination. I really don't understand how the algorithm work
Solution
If it is the solution/example you are are looking for I can refer you to an API I have created to generate combinations of objects in a memory efficient way: https://github.com/3venthorizon/meta/blob/master/meta-mathics/src/main/java/com/devlambda/meta/mathics/CombinationGenerator.java
@Test
public void charComboTest() {
List<Character> domain = Arrays.asList('a', 'b', 'c', 'd', 'e');
CombinationGenerator<Character> combinations = new CombinationGenerator<>(domain, 3);
while (combinations.hasNext()) {
List<Character> combination = combinations.next();
System.out.println(combination.stream().map(Object::toString)
.collect(Collectors.joining(", ", "[", "]")));
}
}
This prints out the the following result:
[a, b, c]
[a, b, d]
[a, b, e]
[a, c, d]
[a, c, e]
[a, d, e]
[b, c, d]
[b, c, e]
[b, d, e]
[c, d, e]
Answered By - Dewald Pretorius