一个数组的内存图

  1. package com.besttest.class1;
  2. public class Demo01Array {
  3. public static void main(String[] args) {
  4. int [] arr1= new int[3];
  5. System.out.println(arr1); //地址值
  6. System.out.println(arr1[0]);
  7. System.out.println(arr1[1]);
  8. System.out.println(arr1[2]);
  9. //改变数组当中元素的内容
  10. arr1[1]=10;
  11. arr1[2]=20;
  12. System.out.println(arr1[0]);
  13. System.out.println(arr1[1]);
  14. System.out.println(arr1[2]);
  15. }
  16. }

image.png

两个数组的内存图

  1. package com.besttest.class1;
  2. public class Demo01Array {
  3. public static void main(String[] args) {
  4. int [] arr1= new int[3];
  5. System.out.println(arr1); //地址值
  6. System.out.println(arr1[0]);
  7. System.out.println(arr1[1]);
  8. System.out.println(arr1[2]);
  9. //改变数组当中元素的内容
  10. arr1[1]=10;
  11. arr1[2]=20;
  12. System.out.println(arr1[0]);
  13. System.out.println(arr1[1]);
  14. System.out.println(arr1[2]);
  15. int [] arr2= new int[3];
  16. System.out.println(arr2); //地址值
  17. System.out.println(arr2[0]);
  18. System.out.println(arr2[1]);
  19. System.out.println(arr2[2]);
  20. //改变数组当中元素的内容
  21. arr2[1]=10;
  22. arr2[2]=20;
  23. System.out.println(arr2[0]);
  24. System.out.println(arr2[1]);
  25. System.out.println(arr2[2]);
  26. }
  27. }

image.png

两个引用指向同一个数组

  1. package com.besttest.class1;
  2. public class Demo01Array {
  3. public static void main(String[] args) {
  4. int [] arr1= new int[3];
  5. System.out.println(arr1); //地址值
  6. System.out.println(arr1[0]);
  7. System.out.println(arr1[1]);
  8. System.out.println(arr1[2]);
  9. //改变数组当中元素的内容
  10. arr1[1]=10;
  11. arr1[2]=20;
  12. System.out.println(arr1[0]);
  13. System.out.println(arr1[1]);
  14. System.out.println(arr1[2]);
  15. //将arr1数组的地址值赋值给arr2
  16. int [] arr2= arr1;
  17. System.out.println(arr2); //地址值
  18. System.out.println(arr2[0]);
  19. System.out.println(arr2[1]);
  20. System.out.println(arr2[2]);
  21. //改变数组当中元素的内容
  22. arr2[1]=100;
  23. arr2[2]=200;
  24. System.out.println(arr2[0]);
  25. System.out.println(arr2[1]);
  26. System.out.println(arr2[2]);
  27. }
  28. }

image.png