public class Day04 { public static void main(String[] args) { //单个数组 //定义一个数组 int[] arr = new int[3]; //输出数组名及元素 System.out.println(arr); System.out.println(arr[0]); System.out.println(arr[1]); System.out.println(arr[2]); //给数组中的元素赋值 arr[0] = 100; arr[2] = 200; //再次输出数组名及元素 System.out.println(arr);//[I@1b6d3586 System.out.println(arr[0]);//100 System.out.println(arr[1]);//0 System.out.println(arr[2]);//200 System.out.println("================================================"); /* 多个数组指向相同内存 */ //定义一个数组 int[] arr2 = new int[3]; arr2[0] = 100; arr2[1] = 200; arr2[2] = 300; System.out.println(arr2);//[I@4554617c System.out.println(arr2[0]);//100 System.out.println(arr2[1]);//200 System.out.println(arr2[2]);//300 //定义第二个数组指向第一个数组 int[] arr3 = arr2; arr3[0] = 111; arr3[1] = 222; arr3[2] = 333; //输出两个数组的名及元素 System.out.println(arr2);//[I@4554617c System.out.println(arr2[0]);//111 System.out.println(arr3);//[I@4554617c System.out.println(arr3[0]);//111 } }
单个数组内存图与多个数组指向相同内存
最新推荐文章于 2024-06-24 08:00:00 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)