日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

顺序串的实现

發布時間:2023/12/10 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 顺序串的实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

順序串

  • 接口
  • 查找增加...
  • 測試

接口

package com.lovely.string;/*** * @author echo lovely* 2020年6月9日下午6:44:31** 串的接口描述*/ public interface IString {public void clear();public boolean isEmpty(); public int length(); public char charAt(int i) throws Exception; // 讀取并返回串中的第i個數據元素public IString subString(int begin, int end); // 返回位序號從begin到end-1的子串public void insert(int i, IString str) throws Exception; // 在第i個字符之前插入字串strpublic void delete(int begin, int end); // 刪除位序號從begin到end-1的子串public IString concat(IString str); // 將str連接到字符串的后面public int compareTo(IString str); // 比較str和當前字符串的大小public int indexOf(IString str, int fromIndex) throws Exception; // 從位序號為begin的字符開始搜索與str相等的字串 }

查找增加…

package com.lovely.string;/*** @author echo lovely* 2020年6月9日下午6:55:32* 順序串的實現*/ 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 {// 返回i的位置if (i < 0 || i >= curLen) throw new StringIndexOutOfBoundsException(i);return strValue[i];}// 將串的長度擴充為newCapacitypublic void allocate(int newCapacity) {if (newCapacity <= curLen) {System.err.print("擴充長度不能比當前長度小!");return;}char[] tmp = strValue;strValue = new char[newCapacity];// 剩下長度newCapacity - tmp.lengthfor (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;}@Overridepublic void delete(int begin, int end) {if (begin < 0 || end > curLen || begin >= end) {throw new StringIndexOutOfBoundsException("參數非法");}/*don't konw...*/}@Overridepublic IString concat(IString str) {// 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) { // 從主串的第i個元素開始比較for (int j = 0; j < len; j++) { // 比較模式串的元素if (str.charAt(j) != strValue[j + i]) {i++;break; // 跳出當前循環} else if (j == len - 1) { // len個字符全部匹配成功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;/*** * @author echo lovely* 2020年6月10日下午3:37:30* * 串的測試demo*/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'}; // 插入串1下標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 最后一個字符串 部

總結

以上是生活随笔為你收集整理的顺序串的实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。