生活随笔
收集整理的這篇文章主要介紹了
顺序串的实现
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
順序串
接口
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("參數(shù)不合法");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
;}@Overridepublic void delete(int begin
, int end
) {if (begin
< 0 || end
> curLen
|| begin
>= end
) {throw new StringIndexOutOfBoundsException("參數(shù)非法");}}@Overridepublic 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
最后一個字符串 部
總結
以上是生活随笔為你收集整理的顺序串的实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。