当前位置:网站首页>Variable string
Variable string
2022-08-04 01:32:00 【Protect milk cat】
可变字符串
Variable string usingtoString()Into the immutable strings(Transformation is used to compare values after the same)
因为String类在创建对象时,值不可变.In a frequently updated value is frequent to create objects in memory,效率很低.This is where the variable string
运行下面代码,We can find the efficiency gap,能清晰看出StringIn the low efficiency when frequently updated,浪费内存空间
String str = "";
long begin = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
str += i;
}
long end = System.currentTimeMillis();
//The output is used by cycle time
System.out.println(end - begin);
StringBuilder sb = new StringBuilder();
long begin = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
sb.append(i);
}
long end = System.currentTimeMillis();
//The output is used by cycle time
System.out.println(end - begin);
StringBuilder类
概念
A class used to represent a variable string,是非线程安全的,Suggest to use in a single-threaded environment,效率略高于
StringBuffer.
| 构造方法 | |
|---|---|
| new StringBuilder() | 默认创建一个长度为16的字符数组 |
| new StringBuilder(int i) | 创建一个长度为i的字符数组 |
| new StringBuilder(String str) | 创建一个长度为str.length()+16的数组 |
StringBuilder sb = new StringBuilder();
sb.append("xjc");
sb.append(123);
sb.append(4.0);
sb.append(new Random());
System.out.println(sb);
xjc1234.0java.util.Random@74a14482
根据结果可以看出append()Is used for string add content,可以是任何内容
常用方法(在使用 StringBuilder 类时,每次都会对 StringBuilder 对象本身进行操作,而不是生成新的对象,不同于String的方法,Call after the results the same):
sb.append("qwert");
//删除[0,3)
sb.delete(0,3);
//删除指定位置
sb.deleteCharAt(1);
System.out.println(sb);
//从0开始插入
sb.insert(0,"asd");
System.out.println(sb);
//[0,3)用*替换
sb.replace(0,3,"*");
System.out.println(sb);
//反转
sb.reverse();
System.out.println(sb);
StringBuffer类
A class used to represent a variable string,是线程安全的,建议在多线程环境下使用,效率略低于StringBuilder.
StringBuilder和StringBufferAll methods in the role,只不过StringBuffer中的方法使用了synchronized关
Key to modify,Said a synchronization method,在多线程环境下不会出现问题.
StringBuilder是一个可变的字符序列.此类提供一个与 StringBuffer 兼容的 API,但不保证同步.该类被设计用作 StringBuffer 的一个简易替换,用在字符串缓冲区被单个线程使用的时候(这种情况很普遍).
StringBuiffer也代表可变字符串对象.实际上,StringBuilder和StringBuffer基本相似,两个类的构造器和方法也基本相同.不同的是:StringBuffer是线程安全的,而StringBuilder则没有实现线程安全功能,所以性能略高.
包装类
Eight basic data types are wrapper classes
装箱
概念:All the wrapper class has a static methodvalueOf(原始类型),Converts a primitive type of data to the corresponding wrapper class object,这个过程称为装箱.
//手动装箱
int i=123;//Define a primitive type of data
Integer aInteger=Integer.valueOf(i);//调用包装类的valueOf()Method converts the original type package Class objects of
拆箱
概念:
All the wrapper class has a primitive typeValue()方法,Class object is used to pack into the original type,这个过程称为拆箱
//手动拆箱
Integer aInteger=new Integer(123);//创建一个包Class objects of
int i = aInteger.intValue();//调用包装类的"原始类型Value()"Method converts it to the original type
//自动装箱
Integer aInteger=123;
//自动拆箱
int i=aInteger;
自动装箱池
//以下代码的输出结果:
Integer i1=new Integer(100);
Integer i2=new Integer(100);
//i3中保存的100,在byte范围内,保存在"自动装箱池"中
Integer i3=100;
//i4中保存的100,在byte范围内,如果有现成的,直接引用
Integer i4=100;
//i5和i6中保存的200,超出byte范围,Will create two new object
Integer i5=200;
Integer i6=200;
System.out.println(i1==i2);//false
//i3和i4保存的是同一个地址,所以是true
System.out.println(i3==i4);//true
//i5和i6Save is two address,所以是false
System.out.println(i5==i6);//false
//Involves the comparison of wrapper classes,To use the wrapper class overrides theequals方法
System.out.println(i5.equals(i6));
If by calling the constructor to create a wrapper class object,Belongs to a different address,使用==比较的结果为false
The form of automatic packing,赋值范围在-128~127之间,To save the number within the scope of the"自动装箱池"中,
So two class object to save the number of packaging within the scope of this,使用同一个地址.超过这个范围,Use is not the same address.
So we compare between reference type,一定不要使用**==,You want to use rewriteequals**方法
习题
输入一段字符串,拼接n次,统计每个字符出现的次数
解法一(Can't statistics in Chinese):
Scanner sc = new Scanner(System.in);
System.out.println("输入一段英文字符:");
String str = sc.next();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 20; i++) {
sb.append(str);
}
char[] chars = sb.toString().toCharArray();
int[] list = new int[123];
for (int i = 0; i < chars.length; i++) {
//char[0]: a
//chars[i]Every letter is corresponding to theascii码,即是list[a]--list[97]
list[chars[i]]++;
}
for (int i = 65; i < list.length; i++) {
if (list[i] != 0){
System.out.println((char)i+":"+list[i]);
}
}
解法二:
Scanner sc = new Scanner(System.in);
System.out.println("输入一段英文字符:");
String str = sc.next();
//Define a character array to store a string of words
char[] letters = new char[100];
for (int i = 0;i < str.length();i++){
//Remove the string every
letters[i] = str.charAt(i);
//Cycle to judge whether the newly added a front and repeat
for (int j = 0;j < i;j++){
//Repeat to make it as0
if (letters[i] == letters[j]) {
letters[i] = 0;
break;
}
}
}
// for (char l : letters) {
// if (l != 0){
// System.out.println(l);
// }
// }
StringBuilder sb = new StringBuilder();
System.out.println("Number of input together");
int n = sc.nextInt();
//Will the string concatenation in front of the
for (int i = 0; i < n; i++) {
sb.append(str);
}
//StringBuilder转化为字符串
String str1 = sb.toString();
//字符串转化为字符数组
char[] str2 = str1.toCharArray();
//To pick up the front of each letter after mosaics of circulation and compare
for (char letter : letters) {
if (letter != 0) {
int count = 0;
for (char c : str2) {
if (letter == c) {
count++;
}
}
System.out.println(letter + ": " + count + "次");
}
}
边栏推荐
- 【虚拟化生态平台】虚拟化平台esxi挂载USB硬盘
- typescript52-简化泛型函数调用
- 【OpenCV】-重映射
- 通用的测试用例编写大全(登录测试/web测试等)
- nodejs install multi-version version switching
- 七夕佳节即将来到,VR全景云游为你神助攻
- Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
- C 学生管理系统_添加学生
- Analysis of usage scenarios of mutex, read-write lock, spin lock, and atomic operation instructions xaddl and cmpxchg
- C# WPF设备监控软件(经典)-下篇
猜你喜欢

数组_滑动窗口 | leecode刷题笔记

网络带宽监控,带宽监控工具哪个好

typescript52 - simplify generic function calls

DDTL:远距离的域迁移学习
![Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.](/img/10/87c0bedd49b5dce6fbcd28ac361145.png)
Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.

Quickly build a website with static files

this巩固训练,从两道执行题加深理解闭包与箭头函数中的this

Flink jdbc connector 源码改造sink之 clickhouse多节点轮询写与性能分析

谁说程序员不懂浪漫,表白代码来啦~

typescript53-泛型约束
随机推荐
initramfs详解----添加硬盘驱动并访问磁盘
持续投入商品研发,叮咚买菜赢在了供应链投入上
ASP.NET 获取数据库的数据并写入到excel表格中
jmeter分布式压测
C 学生管理系统_添加学生
nodejs切换版本使用(不需要卸载重装)
html select标签赋值数据库查询结果
typescript55 - generic constraints
工程制图平面投影练习
typescript48 - type compatibility between functions
thinkphp 常用技巧
typescript52 - simplify generic function calls
Linux安装mysql最简单教程(一次成功)
C 学生管理系统 显示链表信息、删除链表
Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
appium软件自动化测试框架
Observability:你所需要知道的关于 Syslog 的一些知识
无代码7月热讯 | 微软首推数字联络中心平台;全球黑客马拉松...
持续投入商品研发,叮咚买菜赢在了供应链投入上
Installation and configuration of nodejs+npm