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[] 数组内容转换为一 ...
随机推荐
- mysql查询count
Every derived table must have its own alias 每个派生出来的表都必须有一个自己的别名 一般在多表查询时,会出现此错误. 因为,进行嵌套查询的时候子查询出来的的 ...
- tomcat下载及启动
http://tomcat.apache.org/ 打开网页,在左边选择版本,选择后网页往下面拉 拉下来,根据windows选择32还是64位的,其中zip是windows免安装版 下载后解压,然后配 ...
- Qt由pcm数据生成wav文件
void AudioGrabber::saveWave(const QString &fileName, const QByteArray &raw, const QAudioForm ...
- objective-c 中数据类型之四 字典(NSDictionary)
// 1. 字典初始化.赋值方式1 NSMutableDictionary *m_dictionary = [[NSMutableDictionary alloc] initWithCapacity: ...
- ubuntu14.04 安装LNMP
新书上市<深入解析Android 5.0系统> 通常我们使用centos来组建LNMP,可是我们开发时多使用ubuntu的桌面版本号来调试,以下将具体介绍怎样在ubuntu上安装一套LNM ...
- DevExpress14.1.2 xe XE6 高速安装
之前在在网上下载的DevExpress14.1.2 xe-XE6都是一个个包文件.须要一个个去查找编译安装,并且须要有一定的顺序要求. 所下面载了好久了都没有安装. 近期在网上找了个旧版的安装方法.以 ...
- 深度学习利器:TensorFlow在智能终端中的应用——智能边缘计算,云端生成模型给移动端下载,然后用该模型进行预测
前言 深度学习在图像处理.语音识别.自然语言处理领域的应用取得了巨大成功,但是它通常在功能强大的服务器端进行运算.如果智能手机通过网络远程连接服务器,也可以利用深度学习技术,但这样可能会很慢,而且只有 ...
- [JavaEE] Apache Maven 入门篇(上)
http://www.oracle.com/technetwork/cn/community/java/apache-maven-getting-started-1-406235-zhs.html 作 ...
- 图片全屏轮播插件poposlides
jQuery轻量级全屏自适应焦点图插件poposlides 在线演示本地下载
- 【华科考研机试题】最长&最短文本
题目 输入多行字符串,请按照原文本中的顺序输出其中最短和最长的字符串,如果最短和最长的字符串不止一个,请全部输出. 解题思路 1.输入所有字符串(有空格不另算字符串). 2.将char*字符串转换成s ...