当前位置:网站首页>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 + "次");
}
}
边栏推荐
- html select标签赋值数据库查询结果
- 【正则表达式】笔记
- typescript54 - generic constraints
- Apache DolphinScheduler actual combat task scheduling platform - a new generation of distributed workflow
- this巩固训练,从两道执行题加深理解闭包与箭头函数中的this
- WMS仓储管理系统能解决电子行业哪些仓库管理问题
- Tanabata festival coming, VR panoramic look god assists for you
- Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
- redis中常见的问题(缓存穿透,缓存雪崩,缓存击穿,redis淘汰策略)
- Apache DolphinScheduler新一代分布式工作流任务调度平台实战-中
猜你喜欢
字符串的排列
Array_Sliding window | leecode brushing notes
持续投入商品研发,叮咚买菜赢在了供应链投入上
nodejs+npm的安装与配置
2022 China Computing Power Conference released the excellent results of "Innovation Pioneer"
typescript54 - generic constraints
一个注解替换synchronized关键字:分布式场景下实现方法加锁
OpenCV如何实现Sobel边缘检测
Vant3 - click on the corresponding name name to jump to the next page corresponding to the location of the name of the TAB bar
Apache DolphinScheduler新一代分布式工作流任务调度平台实战-中
随机推荐
有没有jdbc 链接优炫数据库文档及示例?
JS 从零教你手写节流throttle
GraphQL背后处理及执行过程是什么
Promise 解决阻塞式同步,将异步变为同步
Deng Qinglin, Alibaba Cloud Technical Expert: Best Practices for Disaster Recovery across Availability Zones and Multiple Lives in Different Locations on the Cloud
typescript56 - generic interface
如何通过API接口从淘宝(或天猫店)复制宝贝到拼多多接口代码对接教程
一个项目的整体测试流程有哪几个阶段?测试方法有哪些?
Web3 安全风险令人生畏?应该如何应对?
FeatureNotFound( bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested:
Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
【OpenCV】-重映射
HBuilderX的下载安装和创建/运行项目
FeatureNotFound( bs4.FeatureNotFound: Couldn‘t find a tree builder with the features you requested:
Installation and configuration of nodejs+npm
静态文件快速建站
Vant3 - click on the corresponding name name to jump to the next page corresponding to the location of the name of the TAB bar
【虚拟化生态平台】虚拟化平台esxi挂载USB硬盘
C 学生管理系统_添加学生
typescript58-泛型类