1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| package com.ibm.pdp.util.test;
import java.util.Arrays;
public class AnyTest
{
public static void main( String[] args )
{
String[] tokens = { "Hello", "wonderful", "giant", "world", "leader", "!!!" };
int count = 3;
Combiner<String> combiner = new Combiner<String>( count, tokens );
String[] result = new String[count];
while ( combiner.searchNext( result ) )
System.out.println( Arrays.toString( result ) );
}
public static class Combiner<T>
{
protected int count;
protected T[] array;
protected int[] indexes;
public Combiner( int count, T[] array )
{
super();
this.count = count;
this.array = array;
indexes = new int[count];
for ( int i = 0; i < count; i++ )
indexes[i] = i;
}
public boolean searchNext( T[] result )
{
if ( indexes == null )
return false;
int resultIndex = 0;
for ( int index : indexes )
result[resultIndex++] = array[index];
int indexesRank = count-1;
int arrayRank = array.length-1;
while ( indexes[indexesRank] == arrayRank )
{
if ( indexesRank == 0 )
{
indexes = null;
return true;
}
indexesRank--;
arrayRank--;
}
int restartIndex = indexes[indexesRank] + 1;
while ( indexesRank < count )
indexes[indexesRank++] = restartIndex++;
return true;
}
}
} |
Partager