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操作
都不是在原有的字符串上进行的,而是重新生成了一个新的字符串对象。
也就是说进行这些操作后,最原始的字符串并没有被改变。

String 字符串的追加,数组拷贝的更多相关文章

  1. Swift3 - String 字符串、Array 数组、Dictionary 字典的使用

    Swift相关知识,本随笔为 字符串.数组.字典的简单使用,有理解.使用错误的地方望能指正. ///************************************************** ...

  2. java压缩和解压字符串,Byte数组,String

    在网上找到的压缩解压的工具类,可以压缩String字符串 /*** * 压缩GZip * * @param data * @return */ public static byte[] gZip(by ...

  3. String.getBytes()--->字符串转字节数组

    是String的一个方法String的getBytes()方法是得到一个系统默认的编码格式的字节数组getBytes("utf-8") 得到一个UTF-8格式的字节数组把Strin ...

  4. 集合或数组转成String字符串

    1.将集合转成String字符串 String s=""; for (int i = 0; i < numList.size(); i++) { if (s=="& ...

  5. C# int数组转string字符串

    方式一:通过循环数组拼接的方式: int[] types = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; string result = string.Empty ...

  6. string字符串转数组

    /** * THis_is_a_cat * This Is A Cat * * Cat A Is This * @author Administrator * */ public class Test ...

  7. 【转载】 C#使用string.Join快速用特定字符串串联起数组

    在C#中有时候我们的数组元素需要通过一些特定的字符串串联起来,例如将整形Int数组通过逗号快速串联起来成为一个字符串,可以使用String.Join方法.或者一个字符串string类型数组所有元素快速 ...

  8. 099、Java中String类之字符数组与字符串的转换

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  9. 关于string【】 数组 进行 toString() 之后无法将数组的内容连接起来组合成 string 字符串 的问题

    string[] to string 如果直接对一个string[] 数组进行 tostring()的操作,得到的值都是 system.string[] 如果想要将 string[] 数组内容转换为一 ...

随机推荐

  1. jmeter函数和变量

    函数和变量广泛的应用在JMeter的传参过程,其中函数可以被认为是某种特殊的变量,它们可以被采样器或者其他测试元件所引用. 常用函数 1.__RamdomString() / __Ramdom() 获 ...

  2. const指针总结

    const 总结: 假设keywordconst出如今星号左边.表示被指物是常量:即不能通过指针改动变量的值. 假设keywordconst出如今星号右边,表示指针自身是常量:即不能改变指针的指向. ...

  3. PHP反射类的理解(代码篇)

    <?php/** * Created by PhpStorm. * User: * Date: 2017/6/12 * Time: 14:34 * 关于反射类的理解 */class Person ...

  4. scanf,printf函数细节

    今天笔试的时候遇到一个考察C语言scanf函数的题目 int x; float y; scanf("%3d%f",&x,&y); // input 123456 6 ...

  5. 用BFS解决迷宫问题

    在一个n*n的矩阵里走,从原点(0,0)開始走到终点(n-1,n-1),仅仅能上下左右4个方向走.仅仅能在给定的矩阵里走,求最短步数. n*n是01矩阵,0代表该格子没有障碍.为1表示有障碍物. in ...

  6. 0x53 区间DP

    石子合并 搞笑 #include<cstdio> #include<iostream> #include<cstring> #include<cstdlib& ...

  7. 深度学习利器:TensorFlow在智能终端中的应用——智能边缘计算,云端生成模型给移动端下载,然后用该模型进行预测

    前言 深度学习在图像处理.语音识别.自然语言处理领域的应用取得了巨大成功,但是它通常在功能强大的服务器端进行运算.如果智能手机通过网络远程连接服务器,也可以利用深度学习技术,但这样可能会很慢,而且只有 ...

  8. 随时随地日志Debug

    对于一个应用程序而言,Log必不可少,但是有些时候仅仅想看下输出,如果加log的话就显得比较麻烦,这个时候就用到了Debug.WriteLine("测试下,你好,非常棒,牛叉!") ...

  9. website robots.txt 防爬虫 措施

    robots.txt文件用法举例: 1. 允许所有的robot访问 User-agent: * Allow: / 或者 User-agent: * Disallow: 2. 禁止所有搜索引擎访问网站的 ...

  10. VS2015启动显示ID为XXXX的进程当前未运行

    解决办法:在启动项目根目录下用文本编辑器打开Web项目下的{X}.csproj文件,然后查找 <WebProjectProperties>,将这一对标签之间的内容全部删除,然后再打开项目就 ...