当前位置:网站首页>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 + "次");
}
}
边栏推荐
猜你喜欢
随机推荐
字符串的排列
Vant3—— 点击对应的name名称跳转到下一页对应的tab栏的name的位置
nodejs+express实现数据库mysql的访问,并展示数据到页面上
boot issue
typescript57 - Array generic interface
Is there any jdbc link to Youxuan database documentation and examples?
LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之三:两次优化
Vant3 - click on the corresponding name name to jump to the next page corresponding to the location of the name of the TAB bar
Jmeter cross-platform operation CSV files
typescript52 - simplify generic function calls
typescript56 - generic interface
互斥锁、读写锁、自旋锁,以及原子操作指令xaddl、cmpxchg的使用场景剖析
持续投入商品研发,叮咚买菜赢在了供应链投入上
Array_Sliding window | leecode brushing notes
600MHz频段来了,它会是新的黄金频段吗?
如何用C语言代码实现商品管理系统开发
【OpenCV】-重映射
Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
initramfs详解----添加硬盘驱动并访问磁盘
FeatureNotFound( bs4.FeatureNotFound: Couldn‘t find a tree builder with the features you requested: