当前位置:网站首页>Thinking about the transformation between string and char[]
Thinking about the transformation between string and char[]
2022-07-22 16:21:00 【Zhi Zi】
If it changes char[]
Array ,String
It doesn't change
public class Main {
public static void main(String[] args) {
char[] cs = "Hello".toCharArray();
String s = new String(cs);
System.out.println(s);
cs[0] = 'X';
System.out.println(s);
}
}
Output :
Hello
Hello
That's because through new String(char[])
Create a new String
When an instance , It doesn't directly refer to the incoming char[]
Array , It's a copy , therefore , Modify the external char[]
Arrays do not affect String
Inside the instance char[]
Array , Because these are two different arrays .
from String
The invariance design can be seen , If the incoming object is likely to change , We Need copy instead of direct reference .
for example , The following code designs a Score
Class saves the grades of a group of students :
// int[]
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] scores = new int[] { 88, 77, 51, 66 };
Score s = new Score(scores);
s.printScores();
scores[2] = 99;
s.printScores();
}
}
class Score {
private int[] scores;
public Score(int[] scores) {
this.scores = scores;
}
public void printScores() {
System.out.println(Arrays.toString(scores));
}
}
Output :
[88, 77, 51, 66]
[88, 77, 99, 66]
Observe the output twice , because Score
The internal directly refers to the external incoming int[]
Array , This will cause external code to int[]
Array modification , Affect the Score
Class field . If the external code is not trusted , This will cause security risks .
Please repair Score
Construction method of , So that the modification of the array by external code does not affect Score
Example of int[]
Field .
// int[]
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] scores = new int[] { 88, 77, 51, 66 };
Score s = new Score(scores);
s.printScores();
scores[2] = 99;
s.printScores();
}
}
class Score {
private int[] scoreAll;
public Score(int[] scoreAll) {
int[] ints = new int[scoreAll.length];
for (int key = 0; key < scoreAll.length; key++) {
ints[key] = scoreAll[key];
}
System.out.println(ints);
this.scoreAll = ints;
}
public void printScores() {
System.out.println(Arrays.toString(scoreAll));
}
}
Output :
[88, 77, 51, 66]
[88, 77, 51, 66]
边栏推荐
- 什么是单片机?单片机的组成部分有哪些?
- The appearance sequence of question 38 in C language. Three methods (traversal method, recursive method and wolf killing method)
- Remember a composer dependency problem requires composer runtime API ^2.0.0 - > no matching package found
- Elephant Swap的LaaS方案迅速崛起,构建全新DeFi2.0协议
- 嵌套子查询
- C#服务器NFS共享文件夹搭建与上传图片文件
- Chant Developer Workbench 2022
- JVM内存模型:运行时数据区及线程
- Query and association of flask to database
- MP查询条件
猜你喜欢
Session共享问题
【解决】npm ERR! code E401
Mock模拟数据,并发起get,post请求(保姆级教程,一定能成功)
M using similink simulation module to realize dynamic simulation of multipath channel
硬件工程师有没有35岁危机?
The principle of distributed transaction is simple, and it is full of holes
JVM内存模型:类加载器的分类和获取
图计算-图简介
SOC custom IP core -- breathing lamp
STM32基于HAL库的非DMA的轮询ADC单通道与多通道的采样
随机推荐
The LAAS solution of elephant swap has risen rapidly and built a new defi2.0 protocol
DOM style operation
分布式调度问题
ftp服务器搭建部署与C#实现ftp文件的上传
Purchase instructions for pull-up device and waist cushion
Is there a 35 year old crisis for hardware engineers?
【STK初探】创建一条奔月轨道
[solution] NPM err! code E401
万亿市值公链竞争白热化,新公链还有机会吗?
Why does the system we developed have concurrent bugs? What is the root cause of concurrent bugs?
Nftscan and port3 have reached strategic cooperation in the field of NFT data
【leetcode】
【解决方案】解决paddlepaddle运行强化学习代码时TypeError: fc() got an unexpected keyword argument ‘is_test‘的错误
Sorting out interval greed problems
傍晚的天空
M using similink simulation module to realize dynamic simulation of multipath channel
DC-4-靶场实践
Learning notes of line segment tree
The appearance sequence of question 38 in C language. Three methods (traversal method, recursive method and wolf killing method)
TTL、RS232、485到底能传输多远距离?