I have the problem like that ". Write a short Java program that outputs all possible strings formed by using the character ‘c’, ‘a’,’r’, ‘b’, ‘o’ , and ‘n’ exactly one."..and the code here:
public class StringPermutation {
//----------------------------------------------------------------------------
private static char source[] = {'c', 'a', 'r', 'b', 'o', 'n'};
private static char result[] = new char[6];
private static boolean picked[] = new boolean[6];
//----------------------------------------------------------------------------
public static void main(String[] args) {
// initialize that none of characters was picked
java.util.Arrays.fill(picked, false);
pickCharAt(0);
}
//----------------------------------------------------------------------------
private static void pickCharAt(int position) {
// print out if 6 positions already filled
if (position > 5)
System.out.println(String.valueOf(result));
// pick a remain character for current position
else
for (int i=0; i<6; i++) {
// if the character source[i] still not picked then pick it
if (!picked[i]) {
result[position] = source[i];
picked[i] = true;
// fetch for next position
pickCharAt(position + 1);
// return the character source[i] when recur back
picked[i] = false;
}
}
}
I don't understand the pickChartAt function much..could someone show me how it work..and when it stops?
Thanks....