3.6 Array understand

First, the memory array

  Array reference variable is just a reference, the reference variable can point to any valid memory, only when the reference point to a valid memory array elements can be accessed via the array variable.

The actual object is stored in the stack array (heap) memory; if the array reference to the array object reference variable is a local variable, then it is stored in the stack (Stack) memory. A schematic view of an array stored in memory:

  If you need to access array elements as shown in the heap memory, the program can only be achieved by p [index] form. That array reference variable is the fundamental way to access the stack memory array elements.

Heap memory and stack memory:

Heap (heap) memory: allocation of a permanent, large memory area when the Java virtual machine started. Only a heap memory.

Stack (Stack) Memory: allocating a temporary, small memory region of each run-time method. Each method has its own stack area, when the end of the process, for the stack area will be destroyed.

  When a method execution, each method will establish its own memory stack, variables defined within this method will one by one into the stack memory, with the end of the implementation of the method, this method will also destroy the memory stack . Accordingly, all local variables defined in the method are in the stack memory; application when you create an object, the object will be stored in the data area runtime for repeated use (because the cost of creating large object) this run-time data area is the heap memory . Heap memory objects do not disappear with the end of the method, even after the end of the method, the object can also be a reference to other additional variables referenced (very common in the parameter passing method), then the object is still not destroyed . Only when an object is no variable references it, the system garbage collector will reclaim it at the right time.

   If the array heap memory is not there any reference variables pointing to himself, then the array will be garbage, the array will be occupied by the memory recovery system of the garbage collector. Therefore, in order to allow the garbage collector to reclaim the memory space occupied by an array, the array can be assigned poo is null, it cut off the array references cited relationship between variables and the actual array, the actual array will become garbage.

  As long as the types are compatible, it can make a real array variable points to another array, which would create the illusion of an array of variable length. 

. 1  class in ArrayTest 
 2  {
 . 3      public  static  void main (String [] args) 
 . 4      {
 . 5          // static methods defined in the array 
. 6          int [] = A {l, 3,5 };
 . 7          // dynamic initialization definition array 
. 8          var B = new new  int [. 4 ];
 . 9          
10          System.out.println ( "length of the array b is:" + to b.length);
 . 11          // cycle of the output array a, b 
12 is          for ( int I: a)
 13 is          {
 14              the System. out.print ( "" +I);
 15          }
 16          of System.out.print ( "\ n-" );
 . 17          for ( int I = 0; I <to b.length; I ++ )
 18 is          {
 . 19              of System.out.print ( "" + B [I] );
 20 is              IF (I ==-to b.length. 1 )
 21 is                  of System.out.print ( "\ n-" );
 22 is          }
 23 is          // because a, b are int [] reference type, it is possible to make a point b reference arrays point 
24          b = a;
 25          System.out.println ( "the reference variable points to an array of point a to point b of the array length:" + to b.length);
 26 is      }
 27  }
28 ---------- run Java (capture window) ----------
 29 the length of the array b: 4
 30    . 1. 3. 5
 31 is    0 0 0 0
 32 point to a point array after the reference point of the length of the array variable b: 3
 33 is  
34 is output completion (Processed 0 seconds) - normal termination

 After the variables and b variables are referenced first array, this time in the original array elements heap memory of lost references, into the garbage, waiting for the garbage collector to reclaim it.

Second, the basic types of array initialization

  For basic types of array, the value is directly stored in the array element of the array element, and therefore, when initializing the array, to allocate memory space for the array, and the array element directly into the corresponding array elements.

class PrimitiveArrayTest 
{ 
    public  static  void main (String [] args) 
    { 
        // definition of a type int [] array variable 
        int [] Array;
         // dynamic initialization array, the array length. 5 
        Array = new new  int [. 5 ];
         // cyclic manner each array element assignments 
        for (var I = 0; I <be array.length; I ++ ) 
        { 
            array [I] = I + 10 ; 
        } 
        // cycle of the output array 
        for ( int I: array) 
        { 
            the System. Out.println (I); 
        } 
    } 
}
 ---------- run Java (capture window) ---------- 
10 
. 11 
12 is 
13 is 
14 

output is completed (takes 1 second) - normal termination

 Execution int [] array, this time a stack memory allocation and a heap memory (java virtual machine starts when there is), defines a null reference, the reference variable does not point to any valid memory, it can not know the array length

 array = new int [5], the dynamic initialization, the system will default initialization value for an array of memory storage space allocated and assigned: 0 is assigned to all array elements.

        for (var I = 0; I <be array.length; I ++ ) 
        { 
            Array [I] = I + 10 ; 
        } 
cycles array assignment

Third, the reference type of initialization

The basic types of assignments: Direct the value stored in the variable memory.

Reference type of assignment: the number (first address) of a memory cell of the referenced object stored in the variable.

It cited when referencing the array element type of the array, so the situation is more complex. Each array element is stored in a reference that points to another piece of memory, this memory stores the valid data.

The definition of a Person class

 1 class Person 
 2 {
 3     public int age;
 4     public double height;
 5     //定义一个方法
 6     public void info()
 7     {
 8         System.out.println("年龄:"+age+",身高:"+height);
 9     }
10 }

 下面程序定义一个Person[]数组,接着初始化这个Person[]数组,并为这个数组的每个元素指定值。

 1 public class ReferenceArrayTest
 2 {
 3     public static void mian(Srting[] args)
 4     {
 5         //定义一个sudentd数组,类型Person[]
 6         Person[] students;
 7         //执行初始化
 8         students=new Person[2];
 9 
10         //创建两个Person实例
11         var zhang=new Person();
12         zhang.age=15;
13         zhang.height=158;
14 
15         var lee=new Person();
16         lee.age=16;
17         lee.height=168;
18 
19         students[0]=zhang;
20         students[1]=lee;
21 
22         //下面代码的执行结果完全一样
23         lee.info();
24         students[0].info();
25     }
26 }

 执行Person[] students;代码时,这行代码仅仅在栈内存中定义了一个引用变量,也就是一个指针,这个指针并未指向任何有效内存区。

执行动态初始化,默认由系统指定初始值null。

 students=new Person[2];

 然后代码定义了zhang和lee两个Person实例,实际上分配了四个内存,在栈内存储zhang和lee两个引用变量,还在堆内存储两个Person实例,如下所示:

  students[0]=zhang;
  students[1]=lee;

  从图可以看出zhang和students[0]同时指向一个内存区,且都为引用变量,因此通过zhang和students[0]来访问Person实例的方法和变量效果是一样的,无论修改students[0]指向的实例还是zhang指向的实例,所修改的其实是一个内存区。

四、没有多维数组

  java语言的数组类型还是引用类型,这个引用指向真实的数组内存。数组元素的类型也可以是引用,如果数组元素的引用再次指向真实的数组内存,这种情况看上去就很像多维数组。

  定义一个变量的用法:type varName,type是变量类型。如果定义一个引用变量的数组类型,只需要将这个type具体成int[]。

  二维数组的定义:

type[][] arrayName;

 

从采用上面的语法格式来定义二维数组,但它的实质还是一维数组,只是数组元素还是引用,数组元素里保存的引用指向一位数组。

对二维数组的初始化,同样可以将数组看成以为数组初始化,把这个“二维数组”当成一一维数组,其元素类型是type[]类型,可以采用以下语法进行初始化

arrayName=new type[length][]

 

上面的语法相当于初始化一个一维数组,这个一维数组的长度为length,同样这个一维数组的数组元素是引用类型(数组类型),所以系统为每个元素分配的初始值为null。这个二维数组完全可以当成一维数组:使用new type[length]相当于定义了length个type类型变量;类似的,使用new type[length][]初始化这个数组后,相当于定义了length个type[]类型的变量,当然这个type[]类型的变量都是数组类型,因此需要再一次进行初始化。

 1 class 二维数组
 2 {
 3     public static void main(String[] args)
 4     {
 5         //定义一个二维数组
 6         int[][] a;
 7         //把a当成一维数组进行初始化,初始化a为一个长度为4的数组
 8         //a数组的数组元素有是引用类型
 9         a=new int[4][];
10         //把a当成一维数组,遍历a数组的每个元素
11         for(int i=0;i<a.length;i++)
12         {
13             System.out.println(a[i]);
14         }
15         //初始化a数组的第一个元素
16         a[0]=new int[2];
17         a[0][0]=1;
18         a[0][1]=2;
19 
20         //遍历二维数组a的第一个元素
21         for(int i=0;i<a[0].length;i++)
22         {
23             System.out.println(a[0][i]);
24         }
25     }
26 }

 

[][] a;这一行代码,将在栈里定义一个引用变量,这个变量并未指向任何有效的内存空间,此时堆内存还未为这行代码分配任何存储区。
a=new int[4][];程序对数组进行初始化,这行代码让a变量指向一块长度为4的内存空间,这个长度为4的数组里每个数组元素都是引用类型(数组类型),系统为这些数组元素分配初始值:null。此时数组a在内存中存储的示意图:

 

 

 a[0]=new int[2];
 a[0][0]=1;
 a[0][1]=2;

对一个数组原素a[0]进行初始化

 

 初始化多维数组时,可以只指定左边维的大小;当然也可以一次指定每一维的大小。

1 int[][] b=new int[3[4];

 

上面的代码定义一个b数组变量,这个变量指向一个长度为3的数组,这个数组的每个元素又是一个数组类型,他们各自指向长度为4的int[]数组,每个数组的元素为0。示意图为:

 

还可以使用静态初始化方式来初始化二维数组,使用静态初始化方式来初始化二维数组,二维数组的每个数组元素都是一维数组,因此必须指定多个一维数组作为二维数组的初始值。代码如下:

//使用静态初始化方法来初始化一个二维数组
String[][] str1=new String[][]{new String[3],new String[]{"hello"}}

 

 结论:二维数组是一维数组,其数组元素是一维数组;三维数组其数组元素是二维数组。

 

Guess you like

Origin www.cnblogs.com/weststar/p/12314117.html