串的基本概念:
串(也称作字符串)是由n(n≥0)个字符组成的有限序列。
一个串中任意个连续的字符组成的子序列称为该串的子串。 包含子串的串称为该子串的主串。
一个字符在一个串中的位置序号(为大于等于0的正整数)称为该字符在串中的位置。当且仅当这两个串的值完全相等时,称这两个串相等。
数据集合:串的数据集合可以表示为字符序列s0, s1,… , sn-1,每个数据元素的数据类型为字符类型。
那么我们的串存储结构为这个:
串值长度的第一种表示方法下,串的成员变量应包括如下两项:
char[] value;
int count;
其中,value为存储串值的字符类型数组名,count表示串值的长度。
接下来我们来设计一下我们的串存储结构
myString类:
package org.lixiyuan.mystring;
public class MyString {
private char[] value;
private int count;
/**
* 第一个构造
*/
public MyString(){
value=new char[0];
count=0;
}
/**
* 给出一个字符串,然后给定我们从哪儿开始截取,截取多长,然后把要截图的这个部分复制到新的字符串的要放的那个位置
* @param old
* @param index
* @param count
* @param news
* @param newindex
*/
public static void arrayCopy(char[] old,int index,int count,char[] news ,int newindex) throws Exception{
if(old.length<count+index||count+newindex>news.length){
throw new Exception();
}
for (int i = 0; i <count; i++) {
//System.out.println(old[index]);
news[newindex++]=old[index++];
}
}
/**第二个构造
*
* 实现给一个字符串,然后给出往哪儿截取,截取值
* @param str 给出的字符串
* @param index 从哪儿开始截图
* @param count截取多大
*/
public MyString(char[] str,int index,int counts) throws Exception{
if(index<0||counts<0||index+counts>str.length){
throw new Exception("数组越界");
}
this.value=new char[counts];
this.count=counts;
arrayCopy(str,index,counts,value,0);
}
/**
* 第三个构造 给出一个字符数组,然后自己构建一个字符数组
* @param value
* @throws Exception
*/
public MyString(char[] values) throws Exception{
this.count=values.length;
this.value=new char[count];
arrayCopy(values,0,this.count,this.value,0);
}
/**
* 第四个构造,用我们jdk的本身的string类来构建新的字符串数组
* @param str
*/
public MyString (String str){
this.value=str.toCharArray();
this.count=str.length();
}
@Override
public String toString() {
String str="";
for (int i = 0; i <count; i++) {
str+=value[i];
}
return str;
}
/**
* 获得字符串数组的长度
* @return
*/
public int length(){
return this.count;
}
/**
*
* @param index
* @return
*/
public char charAt(int index) throws Exception{
if(index>0||index>count){
throw new Exception("数组越界");
}
return value[index];
}
/**
* 截取字符串
* @param beginIndex开始的位置
* @param endIndex 截到哪儿
* @return
* @throws Exception
*/
public MyString substring(int beginIndex,int endIndex) throws Exception{
if(beginIndex<0||beginIndex>endIndex){
throw new Exception("数组越界");
}
returnbeginIndex==0&&endIndex==count?this:new MyString(this.value,beginIndex,endIndex-beginIndex) ;
}
/**
* 截取字符串
* @param beginIndex
* @return
* @throws Exception
*/
public MyString substring(int beginIndex) throws Exception{
if(beginIndex<0){
throw new Exception("数组越界");
}
returnbeginIndex==0?this:new MyString(this.value,beginIndex,count-beginIndex) ;
}
/**
* 连接字符串
* @param str
* @return
* @throws Exception
*/
public MyString concat(MyString str) throws Exception{
if(str.length()==0){
return this;
}
intlg=str.length();
char[]strArray=str.toCharArray();
char[] newsvalue=new char[lg+count];
arrayCopy(this.value,0,count,newsvalue,0);
arrayCopy(strArray,0,lg,newsvalue,count);
return new MyString(newsvalue);
}
/**
* 转换成字符串数组
* @return
* @throws Exception
*/
public char[] toCharArray() throws Exception{
char[] c=new char[count];
arrayCopy(this.value,0,count,c,0);
return c;
}
/**
* 插入子字符串
* @param str
* @param pos
* @return
* @throws Exception
*/
public MyString insert(MyString str, int pos) throws Exception{
if(pos<0||pos>str.length()){
throw new Exception("数组越界");
}
MyString s2=this.substring(0,pos);
MyString s3=this.substring(pos);
MyString s4=s2.concat(str);
MyString s5=s4.concat(s3);
return s5;
}
/**
* 删除字符串
* @param beginIndex
* @param endIndex
* @return
* @throws Exception
*/
public MyString delete(int beginIndex,int endIndex) throws Exception{
if(beginIndex<0||beginIndex+endIndex>count||endIndex<beginIndex){
throw new Exception("数组越界");
}
MyString s1=this.substring(0,beginIndex);
MyString s2=this.substring(endIndex);
MyString s3=s1.concat(s2);
return s3;
}
}
测试类:
package org.lixiyuan.mystring;
public class Test {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
char[] str1={'l','i','x','i','y','u','a','n'};
char[] str2={'l','o','v','e'};
System.out.println("字符串数组长度"+str1.length);
MyString s=newMyString(str1,0,8);
System.out.println(s.toString());
System.out.println(s.charAt(0));
System.out.println("______测试substring__________");
MyString s1=new MyString("songdongqin");
MyString s2=s1.substring(0, 3);
System.out.println("substring"+s1.substring(0));
System.out.println(s2);
System.out.println("______测试concat__________");
MyString s3=new MyString("Ilove");
MyString s4=s3.concat(s1);
System.out.println(s4);
System.out.println("______测试insert__________");
MyString s5=new MyString("lixiyuan");
MyString s6=s5.insert(s1,2);
System.out.println("%%%%%%%%%%%%%%%%%%");
System.out.println(s6);
System.out.println("测试delete");
MyString s7=s1.delete(0, 4);
System.out.println(s7);
}
}
测试结果:
字符串数组长度8
lixiyuan
l
______测试substring__________
substringsongdongqin
son
______测试concat__________
Ilovesongdongqin
______测试insert__________
%%%%%%%%%%%%%%%%%%
lisongdongqinxiyuan
测试delete
dongqin