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操作
都不是在原有的字符串上进行的,而是重新生成了一个新的字符串对象。
也就是说进行这些操作后,最原始的字符串并没有被改变。
String 字符串的追加,数组拷贝的更多相关文章
- Swift3 - String 字符串、Array 数组、Dictionary 字典的使用
Swift相关知识,本随笔为 字符串.数组.字典的简单使用,有理解.使用错误的地方望能指正. ///************************************************** ...
- java压缩和解压字符串,Byte数组,String
在网上找到的压缩解压的工具类,可以压缩String字符串 /*** * 压缩GZip * * @param data * @return */ public static byte[] gZip(by ...
- String.getBytes()--->字符串转字节数组
是String的一个方法String的getBytes()方法是得到一个系统默认的编码格式的字节数组getBytes("utf-8") 得到一个UTF-8格式的字节数组把Strin ...
- 集合或数组转成String字符串
1.将集合转成String字符串 String s=""; for (int i = 0; i < numList.size(); i++) { if (s=="& ...
- C# int数组转string字符串
方式一:通过循环数组拼接的方式: int[] types = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; string result = string.Empty ...
- string字符串转数组
/** * THis_is_a_cat * This Is A Cat * * Cat A Is This * @author Administrator * */ public class Test ...
- 【转载】 C#使用string.Join快速用特定字符串串联起数组
在C#中有时候我们的数组元素需要通过一些特定的字符串串联起来,例如将整形Int数组通过逗号快速串联起来成为一个字符串,可以使用String.Join方法.或者一个字符串string类型数组所有元素快速 ...
- 099、Java中String类之字符数组与字符串的转换
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- 关于string【】 数组 进行 toString() 之后无法将数组的内容连接起来组合成 string 字符串 的问题
string[] to string 如果直接对一个string[] 数组进行 tostring()的操作,得到的值都是 system.string[] 如果想要将 string[] 数组内容转换为一 ...
随机推荐
- 【网络协议】ICMP协议、Ping、Traceroute
ICMP协议 ICMP常常被觉得是IP层的一个组成部分,它是网络层的一个协议.它传递差错报文以及其它须要注意的信息.ICMP报文通常被IP层或更高层(TCP.UDP等)使用,它是在IP数据报内 ...
- .NET 开发者必备的工具箱
本文作者Spencer是一名专注于ASP.NET和C#的程序员,他列举了平时工作.在家所使用的大部分开发工具,其中大部分工具都是集中于开发,当然也有一些其它用途的,比如图片处理.文件压缩等. 如果你是 ...
- html转义字符换行以及回车等的使用
欢迎加入前端交流群交流知识&&获取视频资料:749539640 html换行回车转义字符 换行Line feed 回车Carriage Return html中换行转义字符 的使 ...
- 虚拟机CentOS设置IP
虚拟机里Centos7的IP地址查看方法 本地虚拟机安装了CentOS 7,想通过ftp上传文件,发现通过ifconfig,没有inet这个属性 查看ens33网卡的配置:vi /etc/syscon ...
- 如何让音频跟视频在ios跟android上自动播放
如何让音频跟视频在ios跟android上自动播放 <audio autoplay ><source src="audio/alarm1.mp3" type=&q ...
- 2018亚洲区预选赛北京赛站网络赛 D.80 Days 尺取
题面 题意:你带着K元要去n个城市,这n个城市是环形的,你可以选择任意一个起点,然后顺时针走,对于每个城市,到达时可以获得a元,但是从这里离开又需要花费b元,问你能否找到一个起点(输出花钱最少的那个) ...
- V8 引擎是如何工作的?
V8 引擎是如何工作的? 本文翻译自:How the V8 engine works? V8是谷歌德国开发中心构建的一个JavaScript引擎.它是由C++编写的开源项目,同时被客户端(谷歌浏览 ...
- web 端即时通讯
1. 前言 Web端即时通讯技术因受限于浏览器的设计限制,一直以来实现起来并不容易,主流的Web端即时通讯方案大致有4种:传统Ajax短轮询.Comet技术.WebSocket技术.SSE(Serve ...
- MySQL视图、触发器、事务、存储过程、函数
视图.触发器.事务.存储过程.函数 阅读目录 一 视图 二 触发器 三 事务 四 存储过程 五 函数 六 流程控制 一 视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据 ...
- 数据科学的完整学习路径(Python版)
转载自:http://python.jobbole.com/80981/ 英文(原文)连接:https://www.analyticsvidhya.com/learning-paths-data-sc ...