// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
public class Perm {
    public static void writePerm(char[] a, int len, String prefix, int ix, boolean[] used) {
        if(ix < len) {
            for(int i = 0; i < a.length; i++) {
                if(!used[i]) {
                    used[i] = true;
                    writePerm(a, len, prefix + a[i], ix + 1, used);
                    used[i] = false;
                }
            }
        } else {
            System.out.println(prefix);
        }
    }
    public static void writePerm(char[] a) {
        boolean[] used = new boolean[a.length];
        for(int i = 1; i <= a.length; i++) {
            writePerm(a, i, "", 0, used);
        }
    }
    public static void main(String[] args) {
        char[] a = { 'a', 'b', 'c', 'd' };
        writePerm(a);
    }
}
