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[] 数组内容转换为一 ...
随机推荐
- 几个比較好的IT站和开发库官网
几个比較好的IT站和开发库官网 1.IT技术.项目类站点 (1)首推CodeProject,一个国外的IT站点,官网地址为:http://www.codeproject.com,这个站点为程序开发人员 ...
- shell学习五十天----查看进程ps命令
进程列表 列出进程中最重要的命令便是进程状态命令:ps. ps命令是进程状态(Process Status)的缩写.ps命令用来列出系统中当前执行的那些进程.ps命令列出的是当前那些进程的快照,就是执 ...
- J2EE7与Servlet3.x
J2EE7结构图 JWS:即Java Web Service,指与webservice相关的JavaEE技术部分,webservice是一种基于XML的独立的.跨平台的.互操作的应用程序,XML又包含 ...
- SwiftUI 官方教程
SwiftUI 官方教程 完整中文教程及代码请查看 https://github.com/WillieWangWei/SwiftUI-Tutorials SwiftUI 官方教程 SwiftUI ...
- WinForm——操作word文档
解决方案资源管理器——引用——(右击)添加引用——COM 1. 安装Office,添加引用COM里面的 Microsoft Word 14.0 Object. Library 2. 导命名空间 usi ...
- BS程序性能调优
首先想到的是优化算法.改进技术.扩展设备去做优化.其实在讨论性能的时候,绕不开对业务的理解,不同的业务系统对性能的要求不同,优化方式也不一样.优化性能的前提是保证业务的正确性.我们平时关注的性能主要是 ...
- EditPlus代码自动完成的设置
EditPlus代码自动完成的设置保存在 *.acp 文件中,可以在“工具”->“首选项”->“文件”->“文件类型及语法”中(如下图) 其中“语法文件”保存着进行语法高亮的关键词, ...
- 第九课: - 导出到CSV / EXCEL / TXT
第 9 课 将数据从microdost sql数据库导出到cvs,excel和txt文件. In [1]: # Import libraries import pandas as pd import ...
- javascript一个作用域案例分析
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- (转载)Android 方法数超过64k、编译OOM、编译过慢解决方案。
Android 方法数超过64k.编译OOM.编译过慢解决方案. 目前将项目中的leancloud的即时通讯改为环信的即时通讯.当引入easeui的时候 出现方法数超过上限的问题. 搜索一下问题, ...