Peer-reviewed code snippets that anyone can edit
A wiki for useful code snippets
An alphabet sequence generator
r1 r2
1
[code=java]
1
2
public class SeqGen {
2
3
    public static void main(String[] args) {
3
        //This is the configurable param
4
        // This is the configurable param
4
5
        int seqWidth = 3;
5
6
       
6
        Double charSetSize = 26d;
7
        int charSetSize = 26;
7
8
       
8
9
        // The size of the array will be 26 ^ seqWidth. ie: if 2 chars wide, 26
9
10
        // * 26. 3 chars, 26 * 26 * 26
10
        Double total = Math.pow(charSetSize, (new Integer(seqWidth)).doubleValue());
11
        int total = (int)Math.pow(charSetSize, seqWidth);
11
12
       
12
        StringBuilder[] sbArr = new StringBuilder[total.intValue()];
13
        StringBuilder[] sbArr = new StringBuilder[total];
13
14
        // Initializing the Array
14
        for(int j = 0; j <total; j++){
15
        for (int j = 0; j <total; j++) {
15
16
            sbArr[j] = new StringBuilder();
16
17
        }
17
18
       
18
19
        char ch = 'A';
19
20
        // Iterating over the entire length for the 'char width' number of times.
20
21
        // TODO: Can these iterations be reduced?
21
        for(int k = seqWidth; k >0; k--){
22
        for (int k = seqWidth; k > 0; k--) {
22
23
            // Iterating and adding each char to the entire array.        
23
            for(int l = 1; l <=total; l++){
24
                sbArr[l-1].append(ch);
25
                if((l % (Math.pow(charSetSize, k-1d))) == 0){
24
            for (int l = 0; l < total; l++) {
25
                sbArr[l].append(ch);
26
                if (l % Math.pow(charSetSize, k-1) == 0) {
26
27
                    ch++;
27
                    if(ch > 'Z'){
28
                    if (ch > 'Z') {
28
29
                        ch = 'A';
29
30
                    }
30
31
                }
31
32
            }
32
33
        }
33
34
       
34
        //Use the stringbuilder array.
35
        // Use the stringbuilder array.
35
36
        for (StringBuilder builder : sbArr) {
36
37
            System.out.println(builder.toString());
37
38
        }
38
39
    }
39
40
}
41
[/code]
Tags added:
Tags removed: