public StringTest(String substring, int total) {
super();
this.substring = substring;
this.total = total;
}
public String getSubstring() {
return substring;
}
public void setSubstring(String substring) {
this.substring = substring;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String s = "typitypitypingoutandthereareprobabytyposandstuffdontexpectoutoreadthisbutifyoucanthenygoodforyouhohohomerryxmasyesimtoolazytowritearandomstringgenerator ";
// 拆分字符串形成子字符串队列
ArrayList<String> arraylist = new ArrayList();
for (int i = 0; i < s.length() - 4; i++) {
arraylist.add(s.substring(i, i + 4));
}
// 定义字符串 出现次数类的队列
ArrayList<StringTest> slist = new ArrayList();
// 开始查找相同的字符串,如果相同在total上加1并移除该重复出现的子字符串
for (int i = 0; i < arraylist.size() - 2; i++) {
slist.add(new StringTest(arraylist.get(i), 1));
for (int j = i + 1; j < arraylist.size() - 1; j++) {
if (arraylist.get(i).equals(arraylist.get(j))) {
slist.get(i).setTotal(slist.get(i).getTotal() + 1);
arraylist.remove(j);
j--;// 因为arraylist.get(i)已经移除这时原先的arraylist.get(j+1)会移到前面来处于第j的位置
// 因此为了不漏过遍历 把j--一下
}
}
}
// 对子字符串出现的频率进行降序排序
StringTest t = null;
for (int i = 0; i < slist.size() - 1; i++) {
for (int j = slist.size() - 1; j > i; j--) {
if (slist.get(j).getTotal() > slist.get(j - 1).getTotal()) {
t = slist.get(j);
slist.set(j, slist.get(j - 1));
slist.set(j - 1, t);
}
}
}
// 输出每个子窜出现的频率
for (int i = 0; i < slist.size(); i++) {
System.out.println(slist.get(i).getSubstring() + "出现频率是"
+ slist.get(i).getTotal());
}
}