接口
package com. lovely. string;
public interface IString { public void clear ( ) ; public boolean isEmpty ( ) ; public int length ( ) ; public char charAt ( int i) throws Exception; public IString subString ( int begin, int end) ; public void insert ( int i, IString str) throws Exception; public void delete ( int begin, int end) ; public IString concat ( IString str) ; public int compareTo ( IString str) ; public int indexOf ( IString str, int fromIndex) throws Exception;
}
查找增加…
package com. lovely. string;
public class SeqString implements IString { private char [ ] strValue; private int curLen; public SeqString ( ) { strValue = new char [ 0 ] ; curLen = 0 ; } public SeqString ( String str) { strValue = str. toCharArray ( ) ; curLen = strValue. length; } public SeqString ( char [ ] ch) { strValue = new char [ ch. length] ; for ( int i = 0 ; i < ch. length; i++ ) { strValue[ i] = ch[ i] ; } curLen = ch. length; } public void clear ( ) { strValue = new char [ 0 ] ; curLen = 0 ; } public boolean isEmpty ( ) { return curLen == 0 ; } public int length ( ) { return curLen; } public char charAt ( int i) throws Exception { if ( i < 0 || i >= curLen) throw new StringIndexOutOfBoundsException ( i) ; return strValue[ i] ; } public void allocate ( int newCapacity) { if ( newCapacity <= curLen) { System. err. print ( "扩充长度不能比当前长度小!" ) ; return ; } char [ ] tmp = strValue; strValue = new char [ newCapacity] ; for ( int i = 0 ; i < tmp. length; i++ ) { strValue[ i] = tmp[ i] ; } } public IString subString ( int begin, int end) { if ( begin < 0 || begin > end || end > curLen) throw new StringIndexOutOfBoundsException ( "参数不合法" ) ; char [ ] tmp = new char [ end - begin] ; for ( int i = begin; i < end; i ++ ) { tmp[ i - begin] = strValue[ i] ; } return new SeqString ( tmp) ; } public void insert ( int i, IString str) throws Exception { if ( i < 0 || i > curLen) throw new StringIndexOutOfBoundsException ( "插入位置非法" ) ; int len = str. length ( ) ; int newCapacity = len + curLen; allocate ( newCapacity) ; for ( int j = curLen - 1 ; j >= i; j -- ) { strValue[ j + len] = strValue[ j] ; } for ( int j = i; j < i + len; j ++ ) strValue[ j] = str. charAt ( j - i) ; curLen = newCapacity; } @Override public void delete ( int begin, int end) { if ( begin < 0 || end > curLen || begin >= end) { throw new StringIndexOutOfBoundsException ( "参数非法" ) ; } } @Override public IString concat ( IString str) { try { insert ( curLen, str) ; } catch ( Exception e) { e. printStackTrace ( ) ; } return new SeqString ( strValue) ; } public int compareTo ( IString str) { if ( curLen > str. length ( ) ) return 1 ; int n = Math. min ( curLen, str. length ( ) ) ; for ( int i = 0 ; i < n; i ++ ) { try { if ( strValue[ i] > str. charAt ( i) ) return 1 ; if ( strValue[ i] < str. charAt ( i) ) return - 1 ; } catch ( Exception e) { e. printStackTrace ( ) ; } } return 0 ; } public int indexOf ( IString str, int fromIndex) throws Exception { if ( str. length ( ) <= curLen && str != null && curLen > 0 ) { int i = fromIndex; int len = str. length ( ) ; while ( i <= curLen - len) { for ( int j = 0 ; j < len; j++ ) { if ( str. charAt ( j) != strValue[ j + i] ) { i++ ; break ; } else if ( j == len - 1 ) { return i; } } } } return - 1 ; } public void display ( ) { for ( int i = 0 ; i < length ( ) ; i ++ ) System. out. print ( strValue[ i] + "" ) ; System. out. println ( ) ; } }
测试
package com. lovely. string; public class TestSeqString { public static void main ( String[ ] args) { SeqString ss = new SeqString ( ) ; try { ss. insert ( 0 , new SeqString ( "abc" ) ) ; char [ ] ch = { 'd' , 'e' , 'f' } ; ss. insert ( 1 , new SeqString ( ch) ) ; ss. insert ( ss. length ( ) , new SeqString ( "ghi" ) ) ; } catch ( Exception e) { e. printStackTrace ( ) ; } ss. concat ( new SeqString ( "连接到串的尾部" ) ) ; ss. display ( ) ; int i = ss. compareTo ( new SeqString ( "ghi" ) ) ; System. out. println ( i > 0 ? "前面的大" : "后面的大" ) ; IString sub = ss. subString ( 0 , 3 ) ; System. out. println ( "前三个串: " ) ; ( ( SeqString) sub) . display ( ) ; try { System. out. println ( "def的下标 " + ss. indexOf ( new SeqString ( "def" ) , 0 ) ) ; System. out. println ( "最后一个字符串 " + ss. charAt ( ss. length ( ) - 1 ) ) ; } catch ( Exception e) { e. printStackTrace ( ) ; } } }
adefbcghi连接到串的尾部
前面的大
前三个串:
ade
def的下标 1
最后一个字符串 部