串的顺序存储结构

来源:互联网 发布:淘宝客服人工投诉电话 编辑:程序博客网 时间:2024/06/06 01:32

串类

 用顺序存储结构实现串类是设计串类的最好方法。这里给出顺序存储结构实现的串MyString和缓冲串类MyStringBuffer。


MyString 类

package 串;
public class MyString {
private char[] value;//私有成员变量,字符数组
private int count;//私有成员变量,字符个数

/*
* 构造函数1
*/

public MyString(){
value=new char[0];
count=0;
}

/*
* 构造函数2
* value为字符数组,offset为数组起始下标,count为个数
* 即用value数组中从offset下标开始、个数为count的字符串创建对象
*/

public MyString(char[]value,int offset,int count){
if(offset<0){
throw new StringIndexOutOfBoundsException(offset);
}
if(count<0){
throw new StringIndexOutOfBoundsException(count);
}
if(offset>value.length-count){
throw new StringIndexOutOfBoundsException(offset+count);
}
this.value =new char[count];
this.count =count;
arrayCopy(value,offset,this.value ,0,count);
}

/*
* 构造函数3
*/

public MyString(char[]value){
this.count =value.length;
this.value =new char[count];
arrayCopy(value,0,this.value,0,count);
}

/*
* 构造函数4
* 此构造函数是为了串初始化赋值的方便,否则程序中不能使用
* 类似MyString ms4=new MyString("abcdefj");的语句
*/

public  MyString(String str){
char[]chararray=str.toCharArray();
value=chararray;
count=chararray.length;
}

/*
* 字符数组拷贝,src为源串的字符数组,srcPos为源串的起始下标
* dst为目标串的字符数组,dstPos为目标串的起始下标,length为新串的长度
*/

static void arrayCopy(char[]src,int srcPos,char[]dst,int dstPos,int length){
if(src.length-srcPos<length||dst.length-dstPos<length){
throw new StringIndexOutOfBoundsException(length);
}
for(int i=0;i<length;i++){
dst[dstPos++]=src[srcPos++];
}
}

/*
* 取字符
*/

public char charAt(int index){
if(index<0||index>=count){
throw new StringIndexOutOfBoundsException(index);
}
return value[index];
}

/*
* 取串的长度
*/

public int length(){
return count;
}

/*
* 比较
* 若当前对象的串值大于anotherString的串值则函数返回一个正整数;
* 若当前对象的串值等于anotherString的串值则函数返回0;
* 若当前对象的串值小于anotherString的串值则函数返回一个负整数。
*/

public int compareTo(MyString anotherString){
int len1=count;
int len2=anotherString.count;
int n=Math.min(len1, len2);//n为len1 和 len2的较小者
char v1[]=value;
char v2[]=anotherString.value;
int i=0;
//int j=0;

int k=i;
int lim=n+i;
while(k<lim){
char c1=v1[k];
char c2=v2[k];
if(c1!=c2){
return c1-c2;
}
k++;
}
return len1-len2;
}

/*
* 取子串
* 所取子串从下标beginIndex开始至下标endIndex的前一个位置
*/

public MyString substring(int beginIndex,int endIndex){
if(beginIndex<0){
throw new StringIndexOutOfBoundsException(beginIndex);
}
if(endIndex>count){
throw new StringIndexOutOfBoundsException(endIndex);
}
if(beginIndex>endIndex){
throw new StringIndexOutOfBoundsException(endIndex-beginIndex);
}
return ((beginIndex==0)&&(endIndex==count))?this:new MyString(value,beginIndex,endIndex-beginIndex);
}

/*
* 取子串
* 所取子串从下标beginIndex开始,至串的尾端
*/

public MyString substring(int beginIndex){
return substring(beginIndex,count);
}

/*
* 连接
*/

public MyString concat(MyString str){
int otherLen=str.length();
char[]strarray=str.toArray();
if(otherLen==0){
return this;
}
char buf[]=new char[count+otherLen];
arrayCopy(value,0,buf,0,count);
arrayCopy(strarray,0,buf,count,otherLen);
return new MyString(buf);
}

/*
* 插入子串
*/

public MyString insert(MyString str,int pos){
if(pos<0||pos>count){
throw new StringIndexOutOfBoundsException(pos);
}
if(pos!=0){
MyString str1=this.substring(0,pos);
MyString str2=this.substring(pos);
MyString res1=str1.concat(str);
MyString res2=res1.concat(str2);
return res2;
}
else 
return str.concat(this);
}

/*
* 删除子串
* 删除当前对象的下标beginIndex开始至下标endIndex的前一个的子串
*/

public MyString delete(int beginIndex,int endIndex){
if(beginIndex<0){
throw new StringIndexOutOfBoundsException(beginIndex);
}
if(endIndex>count){
throw new StringIndexOutOfBoundsException(endIndex);
}
if(beginIndex>endIndex){
throw new StringIndexOutOfBoundsException(endIndex-beginIndex);
}
if((beginIndex==0)&&(endIndex==count)){
return new MyString();
}
else{
MyString str1=this.substring(0,beginIndex);
MyString str2=this.substring(endIndex);
return str1.concat(str2);
}
}

/*
* 输出串值
*/

public void myPrint(){
for(int i=0;i<count;i++){
System.out.print(value[i]);
}
System.out.println();
}

/*
* 返回字符串数组
*/

public char[] toArray(){
char[]buf=new char[count];
arrayCopy(value,0,buf,0,count);
return buf;
}
}


MyStringTest测试类

package 串;
public class MyStringTest {
public static void main(String[] args) {
char[]var1={'d','u','j','s','o','p'};
char[]var2={'a','s','r','i','o','w','n','v','c','x'};

int length1=var1.length;

MyString ms1=new MyString(var1,0,length1);//用构造函数2
MyString ms2=new MyString(var2);//用构造函数3
MyString ms3=new MyString("lihonglei");//用构造函数4
MyString ms4=new MyString("ahjkdajal");//用构造函数4
MyString ms5=ms1.concat(ms2);//测试连接函数
ms1.myPrint();//测试输出函数
ms5.myPrint();
MyString ms6=ms4.substring(0,4);//测试取子串函数
ms6.myPrint();
MyString ms7=ms4.insert(new MyString("123"), 4);//测试插入函数
ms7.myPrint();
MyString ms8=ms4.delete(3, 6);//测试删除函数
ms8.myPrint();
}
}


MyStringBuffer类称作缓冲串类。MyStringBuffer类和MyString类的不同之处是:对于MyString类,连接、插入子串和删除子串成员函数都不改变原对象的串值;但对于MyStringBuffer类,连接、插入子串和删除子串成员函数都改变原对象的串值。


MyStringBuffer类

package 串;
public class MyStringBuffer {
private char[]value;
private int count;

private void expandCapacity(int newCapacity){//重新申请内存空间
char newValue[]=new char[newCapacity];//重新申请内存
arrayCopy(value,0,newValue,0,count);//复制原字符数组
value=newValue;//让value指向新创建的newvalue数组
}

//数组元素拷贝
static void arrayCopy(char[]src,int srcPos,char[]dst,int dstPos,int length){
if(src.length-srcPos<length||dst.length-dstPos<length){
throw new StringIndexOutOfBoundsException(length);
}
for(int i=0;i<length;i++){
dst[dstPos++]=src[srcPos++];
}
}
//构造函数
public MyStringBuffer(String str){
char[]chararray=str.toCharArray();
value=chararray;
count=chararray.length;
}
//连接
public MyStringBuffer concat(MyStringBuffer str){
int otherLen=str.length();
if(otherLen==0){
return this;
}
expandCapacity(count+otherLen);//重新申请内存空间
arrayCopy(str.toArray(),0,this.toArray(),this.length(),str.length());
count=count+otherLen;
return this;//返回原串
}
//返回字符数组
public char[] toArray(){
return value;
}
//返回字符长度
public int length(){
return count;
}
//输出
public void myPrint(){
for(int i=0;i<count;i++){
System.out.print(value[i]);
}
System.out.println();
}
}

MyStringBufferTset测试类

package 串;
public class MyStringBufferTest {
public static void main(String[] args) {
MyStringBuffer msb1=new MyStringBuffer("ahdjlakfhaj");
MyStringBuffer msb2=new MyStringBuffer("abcdefghijklmn");

System.out.println("msb1的连接前输出值:");
msb1.myPrint();
MyStringBuffer msb3=msb1.concat(msb2);
System.out.println("msb1的连接后输出值:");
msb1.myPrint();//此处msb1的原串值已改变
System.out.println("msb1与msb2连接后输出值:");
msb3.myPrint();
}
}


1 0
原创粉丝点击