以下程序可以输出如:
aaa aab aac ...... aba abb ...... edd ede edf ...... zzx zzy zzz
的连续字符串。
修改START_STRING和END_STRING,也可以输出任意位数的连续字符串,可用于穷举遍历。
代码如下:
1 import java.io.*;
2
3 /**
4 *@author comstep
5 *@website http://www.blogjava.net/tripper
6 */
7
8 public class AToZ
9 {
10 private String str = "";
11 private int length = 0;
12 private char[] ch;
13
14 public AToZ(String str)
15 {
16 this.str = str;
17 this.length = str.length();
18 this.ch = str.toCharArray();
19 }
20
21 public String getNextString()
22 {
23 for (int i = 0, j = length - 1; i < length && j >= 0; i ++)
24 {
25 if (ch[j] != 'z')
26 {
27 ch[j] ++;
28 break;
29 }
30 else
31 {
32 ch[j] = 'a';
33 j --;
34 continue;
35 }
36 }
37
38 return new String(ch);
39 }
40
41 public static void main(String[] args) throws Exception
42 {
43 String START_STRING = "aaa";
44 String END_STRING = "zzz";
45
46 AToZ aTOz = new AToZ(START_STRING);
47
48 FileOutputStream fout = new FileOutputStream("out.txt");
49
50 PrintWriter printWriter = new PrintWriter(fout);
51
52 printWriter.println(START_STRING);
53
54 int count = 1;
55
56 while(true)
57 {
58 START_STRING = aTOz.getNextString();
59
60 printWriter.println(START_STRING);
61
62 System.out.println(START_STRING);
63
64 count ++;
65
66 if (START_STRING.equals(END_STRING))
67 break;
68 }
69
70 printWriter.println("共:" + count + " 个");
71
72 System.out.println("共:" + count + " 个");
73
74 printWriter.close();
75 fout.close();
76 }
77 }
78