• java 数组及数组得内存管理总结


    一:一维数组的声明及初始化

    
    

    数组变量属于引用类型,他的元素可以是引用类型,也可以是基本类型。

            int[] a=new int[3];
            a[0]=1;
            a[1]=2;
            a[2]=3;
            int[] b={1,2,3};
            int c[]={1,2,3};
             //    int d[5];          错误表达方式

    a的内存图:

    b与c引用的内存图与a一样。b与c引用只是a引用得简写。

              

     1 public class Test1 {
     2 
     3     public static void main(String[] args) {
     4         // TODO Auto-generated method stub
     5         Date[]  days=new Date[3];
     6         days[0]=new Date(28,8,2014);
     7         days[1]=new Date(26,4,2012);
     8         days[2]=new Date(3,4,2008);
     9     }
    10 }
    11 class Date{
    12 
    13     private int day;
    14     private int month;
    15     private int year;
    16 
    17     Date(int day,int month,int year){
    18         this.day=day;
    19         this.month=month;
    20         this.year=year;
    21     }
    22 }

    二  二维数组

    二位数组的声明与初始化是由高维到低维。

    1         int[][] a=new int[3][];
    2         a[0]=new int[2];
    3         a[1]=new int[3];
    4         a[2]=new int[1];
    5         for(int i=0;i<a.length;i++)
    6             for(int j=0;j<a[i].length;j++)
    7                 a[i][j]=j+1;
    8         int [][]b={{1,2},{1,2,3},{1}};

    b引用与a引用内存分配一致

  • 相关阅读:
    spring-boot4
    spring-boot3代码
    spring-boot3
    spring-boot2代码
    spring-boot2
    Java Socket编程
    eclipse项目中.classpath文件详解
    spring-boot1
    ASCII 在线转换器
    如何在Android开发中让你的代码更有效率
  • 原文地址:https://www.cnblogs.com/shangshicc/p/3901684.html
Copyright © 2020-2023  润新知