当前位置:网站首页>Copy of array and array address value
Copy of array and array address value
2022-07-24 13:08:00 【Cording..】
A simple example of copying array address values :1
/*
* Use a simple array
(1) Create a file called ArrayTest02 Class , stay main() Method declaration arr1 and arr2 Two variables , They are int[] An array of types .
(2) Use braces {}, hold array1 Initialize to 8 Prime number :2,3,5,7,11,13,17,19.
(3) Show arr1 The content of .
(4) assignment arr2 The variable is equal to arr1, modify arr2 Even index elements in , Make it equal to the index value ( Such as arr[0]=0,arr[2]=2). Print out arr1.
*
* Question and answer :arr1 and arr2 What's the relationship ?arr1 and arr2 The address values are the same , All point to the only array entity in the heap space .
*/
public class ArrayTest02 {
public static void main(String[] args) {
int[] arr1,arr2;// Define two arrays
arr1 = new int[]{2,3,5,7,11,13,17,19};// Array one assignment
// Show array1 The content of
System.out.println(" Array before assignment arr1 Value ");
for(int i = 0;i < arr1.length;i++){//arr1.length Get array arr1 The length of , The value is 8
System.out.print(arr1[i] + "\t");
}
// assignment arr2 The variable is equal to arr1
// Cannot be called a copy of an array .
arr2 = arr1;// Array equivalent arr1 The address value of is assigned to the array arr2, At this time, they only want the same array in space
// modify array2 Even index elements in , Make it equal to the index value ( Such as array[0]=0,array[2]=2)
for(int i = 0;i < arr2.length;i++){
if(i % 2 == 0){
arr2[i] = i;
}
}
System.out.println();
// Print out arr1
System.out.println(" Array after assignment arr1 Value ");
for(int i = 0;i < arr1.length;i++){
System.out.print(arr1[i] + "\t");
}
}
}
The results are shown in the figure , We found that we changed the array in the code arr2 When the value of is output arr1 But it changed , Because arr1,arr2 The same address value points to the same array in memory space, that is {2,3,5,7,11,13,17,19}, Change the array arr2 The array becomes {0,3,2,7,4,13,6,19}, Output arr1 It's also changing .

/*2
* Use a simple array
* expand : Modify the title , Realization array2 Yes array1 Copy of array
*/
/*
* Use a simple array
* expand : Modify the title , Realization array2 Yes array1 Copy of array
*/
public class ArrayExer3 {
public static void main(String[] args) { //alt + /
int[] array1,array2;
array1 = new int[]{2,3,5,7,11,13,17,19};
// Show array1 The content of
System.out.println(" Array before assignment arr1 Value ");
for(int i = 0;i < array1.length;i++){
System.out.print(array1[i] + "\t");
}
// Copy of array :
array2 = new int[array1.length];
for(int i = 0;i < array2.length;i++){
array2[i] = array1[i];
}// Each value in the two arrays corresponds to and is equal
// modify array2 Even index elements in , Make it equal to the index value ( Such as array[0]=0,array[2]=2)
for(int i = 0;i < array2.length;i++){
if(i % 2 == 0){
array2[i] = i;
}
}
System.out.println();
// Print out array1
System.out.println(" Array after assignment arr1 Value ");
for(int i = 0;i < array1.length;i++){
System.out.print(array1[i] + "\t");
}
System.out.println();
System.out.println(" Array after assignment arr2 Value ");
for(int i = 0;i < array2.length;i++){
System.out.print(array2[i] + "\t");
}
}
}
At this time, the output result is equivalent to now arr1 The array is assigned to arr2, then arr2 Change the value in the array

边栏推荐
- [datasheet phy] interpretation of ksz8081 data manual
- The EAS BOS development environment client cannot be started, but the server does show that it is ready
- Is it safe to open an account on Oriental Fortune online? Is there a threshold for opening an account?
- Cluster construction based on kubernetes v1.24.0 (III)
- Cluster construction based on kubernetes v1.24.0 (I)
- Generator and async solve asynchronous programming
- 1.9. touch pad test
- 以Chef和Ansible为例快速入门服务器配置
- Relevant laws of animation movement (judge where to move according to parameters)
- 如何使用autofs挂载NFS共享
猜你喜欢

国产旗舰手机定价近六千,却连iPhone12都打不过,用户选谁很明确

20201127 use markdown to draw UML diagrams, graphviz installation experience hematemesis finishing

深圳地铁12号线第二批工程验收通过 预计7月28日试运行

Modern data architecture selection: Data fabric, data mesh

Data + AI summit 2022 PPT download

现代数据架构选型:Data Fabric、Data Mesh

About thread (5) thread pool

2022.07.21
![[datasheet] interpretation of cs5480 data book of metering chip](/img/1a/e8a4ce5c393a6634b6dc8bf6d687e2.png)
[datasheet] interpretation of cs5480 data book of metering chip

3.实现蛇和基本游戏界面
随机推荐
Finclip's "applet export app" function has been updated again by the company
SSM online campus album management platform
English grammar_ Indefinite pronouns - Overview
Prototype inheritance
23. Spiral matrix
The second batch of projects of Shenzhen Metro Line 12 passed the acceptance and is expected to be put into trial operation on July 28
Introduction to encryption technology
26. Reverse linked list II
EAS approval process related table
More functions and functions of the metauniverse lie in the deep transformation of the traditional way of life and production
手把手教你用 Power BI 实现 4 种可视化图表
About thread (4) thread interaction
Step of product switching to domestic chips, stm32f4 switching to gd32
LeadTools 22 kit LeadTools super set
2022.07.21
Leetcode's 302 weekly rematch
Cluster construction based on kubernetes v1.24.0 (III)
如何画 贝赛尔曲线 以及 样条曲线?
Summary of recent interviews
猿人学第六题