当前位置:网站首页>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 + "次");
}
}
边栏推荐
猜你喜欢

initramfs详解----添加硬盘驱动并访问磁盘

Observability:你所需要知道的关于 Syslog 的一些知识

Quickly build a website with static files

Use nodejs switch version (no need to uninstall and reinstall)

一个注解替换synchronized关键字:分布式场景下实现方法加锁

pygame 中的transform模块

jmeter跨平台运行csv等文件

typescript50-交叉类型和接口之间的类型说明

Vant3—— 点击对应的name名称跳转到下一页对应的tab栏的name的位置

js中常用的几种遍历处理数据的方法梳理
随机推荐
nodejs installation and environment configuration
jmeter distributed stress test
贴纸拼词 —— 记忆化搜索 / 状压DP
即席查询——Presto
MySQL回表指的是什么
如何通过API接口从淘宝(或天猫店)复制宝贝到拼多多接口代码对接教程
redis中常见的问题(缓存穿透,缓存雪崩,缓存击穿,redis淘汰策略)
Jmeter cross-platform operation CSV files
Apache DolphinScheduler新一代分布式工作流任务调度平台实战-中
网页三维虚拟展厅为接入元宇宙平台做基础
谁说程序员不懂浪漫,表白代码来啦~
一个注解替换synchronized关键字:分布式场景下实现方法加锁
[store mall project 01] environment preparation and testing
【Untitled】
多线程 之 JUC 学习篇章一 创建多线程的步骤
splice随机添加和删除的写法
How to find the cause of Fiori Launchpad routing errors by single-step debugging
静态文件快速建站
typescript56-泛型接口
Apache DolphinScheduler新一代分布式工作流任务调度平台实战-中