串的实现

来源:互联网 发布:网络拓扑生成 编辑:程序博客网 时间:2024/05/22 15:11

微笑微笑微笑微笑

主类


public class Question1 {
public static void main(String[] args) {       //串比较
MyString str1=new MyString("怀化学院".toCharArray());
MyString str2=new MyString("怀化师专".toCharArray());
int n=str1.compareTo(str2);
if(n<0){
System.out.println("怀化学院大");
}else if(n>0){
System.out.println("怀化师专大");
}else{
System.out.println("一样大");
}
}
}



public class Question2 {
public static void main(String[] args) {         // 串定位
MyString str1=new MyString("怀化学院计算机科学与工程学院".toCharArray());
MyString str2=new MyString("学院".toCharArray());
   int n=str1.indexOf(str2,0);
if(n==-1){
System.out.println("-----不包含检索内容------");
}else{
System.out.print("在str1中第"+(n+1)+"个位置第一次出现了");
str2.out();
}
}

}


微笑微笑微笑微笑

串接口

public interface IString {
int length();//求串长度
    MyString concat(MyString str);//串的连接
    int compareTo(MyString anotherString);//串的比较
    MyString substring(int start, int end);//求子串
int indexOf(MyString str, int fromIndex);//串定位
MyString append(String str);//串附加
MyString delete(int start, int end);//串删除
MyString insert(int offset,MyString str);//串插入
}




微笑微笑微笑微笑

主方法类  串操作


public class MyString implements IString { //实现串
final char[] value;

//构造函数
public MyString(char value[]){
this.value=Arrays.copyOf(value, value.length);

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

public int length() {//求长度

return value.length;
}



public MyString concat(MyString str) {//串拼接
int strLen = str.length();
        if (strLen == 0) {
            return this;
        }
        int len = value.length;
        char buf[] = Arrays.copyOf(value, len + strLen);//内容以及长度
        //原数组 复制起始  目的数组  目标数组放置的起始位置  复制长度 
        System.arraycopy(str.value, 0, buf, len, str.value.length);
        return new MyString(buf);
}





public int compareTo(MyString str) {//串比较 计较unicode码大小
int len1 = value.length;
        int len2 = str.value.length;
        int lim = Math.min(len1, len2);//求出最小的数组长度
        char v1[] = value;//取出第一个比较
        char v2[] =str.value;


        int k = 0;
        while (k < lim) {
            char c1 = v1[k];
            char c2 = v2[k];
            if (c1 != c2) {//不相等时返回  负数2大  0一样大 正数1大
                return c1 - c2;
            }
            k++;
        }
        return len1 - len2; //全部相等时返回长度的比较结果
}





public MyString substring(int start, int end) { //求子串
   if(start<0){//异常处理
    throw new StringIndexOutOfBoundsException(start);
   }
   if(end>value.length){
    throw new StringIndexOutOfBoundsException(end);
   }
   int sublen=end-start;
   if(sublen<0){
    throw new StringIndexOutOfBoundsException(sublen);
   }
   //调用MYstring的构造函数
        return ((start==0)&&(end==value.length)) ? this :new MyString(value,end,sublen);
    }




    public int indexOf(MyString str, int fromIndex) {//串定位
  if(fromIndex >= value.length){
  return (str.value.length==0?value.length : -1);  
  }
  if(fromIndex < 0){
  fromIndex=0;
  }
  if(str.value.length==0){
  return fromIndex;
  }
  char frist=str.value[0];
  int max = value.length-str.value.length;//当剩余的字符小于所需要查找的字符时就无需再查找
  for(int i=fromIndex;i<=max;i++){
  if(value[i]!=frist){
  while(i<=max && value[i] != frist) {i++;}//查找与目标数组第一个相等的字符
  }
  if(i<=max){//查到第一个后往后继续遍历
  int j=i+1;
  int end=j+str.value.length-1;
  for(int k=1;j<end &&value[j]==str.value[k];j++,k++){
  if(j==end-1){
  return i;//当便利满足时输出第一个符合的字符的位置
  }
  }
  }
  }
 return -1;   
    }
 
   
public MyString replace(char oldChar, char newChar){//替换字符
   for(int p=0;p<value.length;p++){
    if(value[p]==oldChar){
    value[p]=newChar;
    }
   }
   return this;
}


    public MyString delete(int star, int en) {//删除字符
    if(star<0){
    throw new StringIndexOutOfBoundsException(star);
   }
   if(en>value.length){
    throw new StringIndexOutOfBoundsException(en);
   }
   int sublen=en-star;
   if(sublen<0){
    throw new StringIndexOutOfBoundsException(sublen);
   }
   for(int o=en;o<value.length;o++){
    value[star]=value[o];
   }
   int len = value.length-en+star;
        char buff[] = Arrays.copyOf(value, len);
        return new MyString(buff);
    }




    public MyString insert(int offset, MyString str2) {//插入字符
    if(offset<0 || offset>value.length){
    throw new StringIndexOutOfBoundsException("yuejieyichang");
   }
    int strLen = str2.value.length;
        if (strLen == 0) {
            return this;
        }
        int len2 = value.length;
        char buf[] = Arrays.copyOf(value, len2 + strLen);
        System.arraycopy(str2.value, 0, buf, offset, str2.value.length);
        return new MyString(buf);
    }
    
    public char[] out(){//输出字符
     System.out.println(this.value);
 return null;
    }
 


}