数据结构 四 :串
一。最近政府貌似又在改革‘房产税’,说是要抑制房价,心里惯性的认为政府又在挠广大民众的痒了,而且是越挠越痒,房价这个问题很复杂,但我相信这深层次的根本原因无非‘利益’两字,而我也相信房价确实会一直走高,而且n年后房价降下来,唯一的原因就是‘政府赚够了’。二。软件行业流传着一句话‘不要重复造轮子’,而在java领域充斥着无数的轮子,有人说‘java已死 ’,我想活跃在java社区的无数创造轮子的人不会让这件事情发生的。同样,‘sql 已死 ’也在最近几年流行于各大论坛,尤其以BanQ的jdon论坛早已开始推尚'NoSql ',现今nosql确实也已有一定市场,如facebook就采用了Memcached,而新浪则以Memcached做了自己的MemcachedDB,在java客户端则有XMemcached,应该说‘nosql‘是一种趋势,但就注入‘sql已死’危言耸听的话不可能发生,只能是厂商的一种炒作,因为‘经典的永远不会过去’,就比如‘数据结构’,转到正题,今天我也造了一个轮子,山寨的String即MyString。
三,直接贴代码:
package com.ds.test4;public class MyString {char[] chars = null; //char数组,实质本类就是操作这些个charprivate int length; //长度public MyString(){} //这个构造方法没啥用public MyString(String str){chars = str.toCharArray(); //构造时,必须转化为charslength = chars.length; //构造时,取得chars的length}public MyString(char[] newChars){ //同上chars = newChars;length = chars.length;}public MyString concat(MyString str){//concat:连接操作int len2 = str.length();int len = length + len2;char[] strChars = str.getChars();char[] newChars = new char;//使用两char数组的长度之和构造新数组然后依次赋值for(int i=0;i<length;i++){newChars = chars;}for(int i=0;i<len2;i++){newChars = strChars;}return new MyString(newChars);}public MyString subString(int from,int len){//截取子串if(from <0){ throw new RuntimeException("起始值from必须大于0");}if(len>length || len<=0){throw new RuntimeException("len不能大于length 或者 len不能小于0");}char[] newChars = new char;//构造新的char数组,然后依次赋值for(int i=0;i<len;i++){newChars = chars;}return new MyString(newChars);}public MyString subString(int from){//同上return subString(from,length-from);}public int indexOf(MyString str){//indexOf:计算str在串的索引int len = str.length();if(len>length){throw new RuntimeException("str's len too long");}char[] strChars = str.getChars();again:for(int i = 0;i<length;i++){ // 使用了标签跳转boolean ifTrue = true;int theIndex = -1;for(int j=0;j<len;j++){ifTrue = (chars == strChars);if(!ifTrue){continue again; // 跳出到again标签继续循环}theIndex = i;}if(ifTrue){return theIndex;}}return -1;}public MyString replace(MyString from,MyString to){//替换操作MyString s1 = null;MyString s2 = null;MyString s3 = null;MyString s4 = null;int theIndex = indexOf(from);if(theIndex == -1){s4 =this; //there doesn't exist the str: from ,then return this}else{s1 = this.subString(0, theIndex);s2 = to;s3 = this.subString(theIndex+to.length()-1);if(s3.indexOf(from) != -1){ //if there're more from ,loop agains3 = s3.replace(from, to); //使用了迭代循环}s4 = s1.concat(s2).concat(s3); //concat those}return s4;}public MyString strInsert(int pos,MyString str){//pos is the position to insertif(pos<0 || pos>=this.length){throw new RuntimeException("pos不能为小于0或者 不能大于等于length");}if(pos == this.length-1){return this.concat(str);}return this.subString(0, pos+1).concat(str).concat(this.subString(pos+1));}public MyString strDelete(int pos,int len){//指定在pos位置开始删除len个charif(pos == 0){return this.subString(len);}else{if((pos+len)==length){return this.subString(0, pos);}if((pos+len)>length){throw new RuntimeException("pos+len 不能大于 length");}return this.subString(0, pos).concat(this.subString(pos+len));}}public int compareTo(MyString str){//比较大小int len = str.length();if(len>length){ //如果str的长度更大,则一定是大于,即返回1return 1;}else if(len<length){ //如果str的长度更小,则一定是小于,即返回-1return -1;}else{ //如果长度相同,则必须一一对比char[] strChars = str.getChars();for(int i = 0;i<len;i++){if(strChars>chars){//只要一出现大于的情况,马上返回为大于即1return 1; }else if(strChars<chars){//只要一出现大于的情况马上返回为小于即-1return -1;}}}return 0; //到最后都没有判断出来,则他们相等,即返回0}public boolean equals(MyString str){//equalsreturn this.compareTo(str)==0?true:false;//如果compareTo为0 则equal否则为false}public char[] getChars(){return chars;}public String toString(){StringBuilder sb = new StringBuilder("");for(int i=0;i<length;i++){sb.append(chars);}return sb.toString();}public int length(){return length;}}
四。MyString有基本操作:
(1)concat:字符串连接,无非是char数组的操作
(2)subString:也是调用了concat进行操作
(3)indexOf:使用了标签循环,就像C语言的goto语句,先前某位学者提出来的‘goto有害’让goto一度陷入尴尬,不 过我觉得任何技术本身没有正确与否,而是看使用者如何使用,对于有经验的程序员可以甚至可以把 ‘if else ’ 写的优雅可读,我想这就是程序员所谓的境界。
(4)replace:我觉得这个方法实现起来稍微复杂一些,使用了迭代循环
(5)strInsert:也是调用了concat操作
(6)strDelete:也是调用了concat操作
(7)compareTo:对两个对象的char一一对比,只要出现大小情况即马上进行判断,忽略之后的char
(8)equals:调用了conpareTo操作
五。问题:
对于字符串这一个命题,可以引申很多有意思的问题,比如模式匹配,而与之对应的算法有‘首尾匹配算法 ’和KMP算法,对于KMP算法这两天在学习中。对于数据结构,有太多经典的算法,就拿排序来说就有:冒泡,选择,希尔,快速,归并,堆。。。,所以说在算法这块应该是学无止境的!
六。总结:任重道远!
页:
[1]