串
串的基本概念
串是由0個或者多個字符組成的有限序列
記為:s="a11a2……an"
串與線性表的對比
串的邏輯結構:和線性表極為相似
區別:串的數據對象約定是字符集
串的基本操作:和線性表有很大區別
- 線性表的基本操作:以單個元素為操作對象
- 串的基本操作:以串的整體為操作對象
子串與主串
子串
由串中任意個連續的字符組成的子序列
主串
包含子串的串
字符位置
字符在序列中的序號(從1開始)
子串位置
子串的首字符在主串中的位置
串的基本操作
加工型
串復制:將某個串復制給當前串
串連接:將串S1和S2聯接而成一個新串
串替換:用子串x替換當前串中的子串y
插入子串:將子串插到當前串中的某個位置
刪除子串:從當前串中刪除指定子串
大寫轉小寫:將串中的大寫字母轉化為對應小寫字符
小寫轉大寫:將串中的小寫字母轉化為對應大寫字符
串壓縮:將當前串中首部和尾部的所有空格刪除
引用型
判空:判斷當前串是否為空
串比較:判斷當前串與指定串是否相等
求串長:返回當前串的字符個數
求子串:返回串的第i個字符開始的長達k個字符的子串
子串定位:輸出子串在當前串中首次出現的位置
串的順序儲存與實現
在串的順序儲存中,一般有三種方法表示串的長度
(1)用一個變量來表示串的長度
(2)在串尾儲存一個特殊字符作為串的終結符
(3)用數組的0號存在串的長度
串的實現
順序儲存結構的串可以用字符數組來存儲字符數據
串的初始化
public calss string{
private int maxSize =10;//串中字符數組的初始長度
private char[] chars;//存儲元素的數組對象
private int length;//保存串的當前長度
public string(){}
public string(int n){}
public void copy(string t){}
public boolean isEmpty(){}
public int compare(string t){}
public int getLength(){}
...//此處省略部分成員方法
}
getter and setter
public class string {
private int maxSize =10;//串中字符數組的初始長度
private char[] chars;//存儲元素的數組對象
private int length;//保存串的當前長度
public string(){}
public string(int n){
}
public void copy(string t){
}
public boolean isEmpty(){
return length <= 0;
}
public int compare(string t){
return -1;
}
public int getLength(){
return length;
}
public int getMaxSize() {
return maxSize;
}
public void setMaxSize(int maxSize) {
this.maxSize = maxSize;
}
public char[] getChars() {
return chars;
}
public void setChars(char[] chars) {
this.chars = chars;
}
public void setLength(int length) {
this.length = length;
}
}
功能完善
import javax.xml.crypto.dsig.keyinfo.RetrievalMethod;
public class string {
private int maxSize =10;//串中字符數組的初始長度
private char[] chars;//存儲元素的數組對象
private int length;//保存串的當前長度
//串的初始化
//構造一個空串
public string(){
}
//構造一個能保存n個字符串的串
public string(int n){
}
public void copy(string t){
//將串t復制給當前串
if(this.maxSize<t.maxSize){
this.maxSize=t.maxSize;
this.chars = new char[this.maxSize];
}
this.length=0;//初始化當前串的長度
for(int i=0;i<t.getLength();i++){
this.chars[i]=t.chars[i];
this.length++;
}
}
public boolean isEmpty(){
return length <= 0;
}
/**
* compare 串比較
* @param t 字符型
* @return int型 | 若當前串等於串t,則返回值0;若當前串大於串t,則返回1;若當前串小於於串t,則返回-1
* @author xrilang
*/
public int compare(string t){
int i = 0;
while(this.chars[i]==t.chars[i] && i < this.length && i<t.getLength()){i++;}
//若當前串等於串t,則返回值0
if(i==this.length && i==t.length) return 0;
//若當前串大於串t,則返回1
else if(i==t.getLength() && i < this.length) return 1;
//若當前串小於於串t,則返回-1
else return -1;
}
/**
* concat 串聯結
* @param t 字符型
* @author 萌狼藍天
*/
public void concat(string t){
if(this.maxSize<this.length+t.getLength()){
//當前串容量不夠,暫存到數組tempArr
char[] tempArr = new char[this.length];
for(int i=0;i<this.length;i++){
tempArr[i]=this.chars[i];
}
//重設maxSize,使串容量滿足需求
this.maxSize = this.length+t.getLength();
this.chars=new char[this.maxSize];
//恢復當前串的原始狀態
for(int i=0;i<tempArr.length;i++){
this.chars[i]=tempArr[i];
}
}
for(int i=0;i<t.length;i++){
this.chars[this.length]=t.chars[i];
this.length++;
}
}
/**
* subString 求子串
* @param pos int型 索引 所求子串起始位置,不能為負數
* @param len int型 長度 所求子串的長度
* @return string型 返回所求子串,如果不存在則返回null
* @author 萌狼藍天
*/
public string subString(int pos,int len){
if(pos<0) return null;
if(pos+len>=this.length) {
//從位置pos開始不存在長度為len的子串
return null;
}
string temp = new string(len);
for(int i=0;i<len;i++){
temp.chars[i]=this.chars[pos+i];
temp.length ++;
}
return temp;
}
public int getLength(){
return length;
}
public int getMaxSize() {
return maxSize;
}
public void setMaxSize(int maxSize) {
this.maxSize = maxSize;
}
public char[] getChars() {
return chars;
}
public void setChars(char[] chars) {
this.chars = chars;
}
public void setLength(int length) {
this.length = length;
}
}