String 字符串的追加,数组拷贝

package chengbaoDemo;

import java.util.Arrays;
/**
 *需求:数组的扩容以及数据的拷贝 
 *分析:因为String的实质是以字符数组存储的,所以字符串的追加,<br>
 *实质上是数组的扩容,数据的移动(复制)
 * 
 */
public class TestString {
    public static void main(String[] args) {
        String src = new String("src");
        String app = new String("app");
        String newString = copy(src, app);
        System.out.println(newString);
    }
    public static String copy(String src, String app) {
        char srcArray[] = src.toCharArray();

        /*(1)创建一个新的字符数组,数组的大小为原字符串的长度 + 追加的字符串长度,
        并将原字符串拷贝到新数组中*/
    //这个方法是Arrays类的静态方法
char[] buf = Arrays.copyOf(srcArray, src.length() + app.length()); //(2)复制追加字符串的字符到新字符数组中,注意: 源对象和目的对象都是字符数组
     //这个方法是System
System.arraycopy(app.toCharArray(), 0, buf, src.length(), app.length()); //(3)返回新字符串 return new String(buf); } }


注意:String类是final, 是不可继承,不可以改变的;
所以字符串的追击,修改才做都不是在原字符串上修改,而是创建一个新的字符串,
讲原字符串,和追加的字符串数据,拷贝到行的字符串数组,
实质:是数组的扩容,数据的移动
如下面几个String类的方法
public String 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 String(offset + beginIndex, endIndex - beginIndex, value);
 
  
    }
 
  
 
 
  
 public String concat(String str) {
 
  
    int otherLen = str.length();
 
  
    if (otherLen == 0) {
 
  
        return this;
 
  
    }
 
  
    char buf[] = new char[count + otherLen];
 
  
    getChars(0, count, buf, 0);
 
  
    str.getChars(0, otherLen, buf, count);
 
  
    return new String(0, count + otherLen, buf);
 
  
    }
 
  
 public String replace(char oldChar, char newChar) {
 
  
    if (oldChar != newChar) {
 
  
        int len = count;
 
  
        int i = -1;
 
  
        char[] val = value; /* avoid getfield opcode */
 
  
        int off = offset;   /* avoid getfield opcode */
 
  
 
 
  
        while (++i < len) {
 
  
        if (val[off + i] == oldChar) {
 
  
            break;
 
  
        }
 
  
        }
 
  
        if (i < len) {
 
  
        char buf[] = new char[len];
 
  
        for (int j = 0 ; j < i ; j++) {
 
  
            buf[j] = val[off+j];
 
  
        }
 
  
        while (i < len) {
 
  
            char c = val[off + i];
 
  
            buf[i] = (c == oldChar) ? newChar : c;
 
  
            i++;
 
  
        }
 
  
        return new String(0, len, buf);
 
  
        }
 
  
    }
 
  
    return this;


结论:
从上面的三个方法可以看出,无论是sub操、concat还是replace操作
都不是在原有的字符串上进行的,而是重新生成了一个新的字符串对象。
也就是说进行这些操作后,最原始的字符串并没有被改变。

 

转载于:https://www.cnblogs.com/chengbao/p/5183825.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值