一、串的定义、特点
1、串是由字符组成的有限序列,是计算机中常用的一种非数值数据。它是一种特殊的线性表,其特殊性在于线性表中的每个元素是一个字符。
2、抽象数据类型:
package com.list;
/**
* 串的抽象数据类型
* @author wangning
*
*/
public interface SString {
int length(); //返回串的长度
char charAt(int i); //返回第i个字符
SString concat(SString str); //返回当前串与str生成的新的字符串
SString substring(int begin,int end); //返回串中字符序号从begin至end-1的子串
void setCharAt(int i,char ch); //设置第i字符为ch
SString insert(int i,SString str);//在第i个字符处插入str串
SString delete(int end,int begin); //删除从begin到end-1的子串
int indexOf(SString pattern); //返回模式串pattern在串中的首次匹配位置
}
二、串的实现
1、串的顺序存储结构
串的顺序存储结构采用字符数组将串中的字符序列依次连续存储在数组的相邻单元中。使用字符数组有两种方案:
①、数组容量等于串长度。此时,串不易增加字符,通用用于存储串常量。
②、当数组容量大于串长度时,便于增加字符,通常用于存储变量,此时还要有一个整型变量记录串的长度。
顺序存储的串具有随机存取的功能,存取指定位置的字符时间复杂度为O(1)。缺点是插入和删除元素时需要移动元素,平均一定数据量是串长度的一盘。当数组容量不足时,需要重新申请一个更大的数组,并复制原数组中的所有元素。插入和删除操作的时间复杂度为O(n)。
2、串的链式存储结构
串的链式存储结构有单字符链表和块链表两种。单字符链表是每个结点的数据域只包含一个字符的单链表,块链表是每个结点的数据域包含若干个字符的单链表。
链式存储的串,存取指定位置字符的时间复杂度为O(n)。单字符链表虽然插入删除操作不需要移动太多元素,但占用太多的存储空间。块链表的插入和删除需要移动元素,效率极低。
字符串不等同于字符数组,字符串只是采用字符数组作为其存储结构。
3、String类
String类以串常量方式存储和实现串操作,其字符数组容量等于串长,且字符数组声明为final,当构造串对象时,对字符数组进行一次复制,其后不能更改,因此String类不提供插入、删除子串操作。
package com.list.impl;
import com.list.SString;
/**
* 自定义MyString 字符串常量
* @author wangning
*
*/
public final class MyString implements SString {
private final char[] value; //final字符数组,只能赋值
//默认构造函数构造一个空串
public MyString(){
this.value=new char[0];
}
//根据string字符串来构造
public MyString(java.lang.String str){
this.value=str.toCharArray();
}
//以value数组中从begin开始的count的字符构造串对象
public MyString(char[] value,int begin,int count){
this.value=new char[count];
for(int i=begin;i<begin+count;i++){
this.value[i]=value[i];
}
}
public MyString(char[] value){
this(value,0,value.length);
}
public MyString(MyString str){
this.value=str.value;
}
public int length() {
return this.value.length;
}
public char charAt(int i) {
if(i<0 || i>=this.value.length){
throw new StringIndexOutOfBoundsException(i);
}
return this.value[i];
}
public java.lang.String toString(){
return new String(this.value);
}
public SString concat(MyString str) {
if(str==null || str.length()==0){
return this;
}
char[] buffer=new char[this.value.length+str.length()];
//复制当前串
int i;
for(i=0;i<this.value.length;i++){
buffer[i]=this.value[i];
}
for(int j=0;j<str.value.length;j++){
buffer[i+j]=str.value[j];
}
return new MyString(buffer);
}
public SString substring(int begin, int end) {
// TODO Auto-generated method stub
return null;
}
public void setCharAt(int i, char ch) {
// TODO Auto-generated method stub
}
public SString insert(int i, SString str) {
// TODO Auto-generated method stub
return null;
}
public SString delete(int end, int begin) {
// TODO Auto-generated method stub
return null;
}
public int indexOf(MyString pattern) {
// TODO Auto-generated method stub
return 0;
}
public SString insert(int i, MyString str) {
// TODO Auto-generated method stub
return null;
}
public int indexOf(SString pattern) {
// TODO Auto-generated method stub
return 0;
}
}
4、StringBuffer类
StringBuffer类以串变量方式实现字符串功能。器字符数组容量大于串长度。插入删除操作同样需要移动元素,但是对串的操作一次处理一个串。