Two kinds of ways to initialize an array of Java serialization 66-

First, the array

1. type stored in the array elements are unified, each element in the memory space occupied by the size is the same, the first element of the array of memory addresses know, just know that you want to find an element index, you can quickly calculate the offset by the first element of the memory address plus an offset, you can quickly calculate the element you want to find the memory address. Quickly locate the element by the memory address, so the higher the efficiency of the array to find the element.

2. random additions and deletions to the array elements when adding the element of time, in order to ensure that elements in the array on the storage space is ordered, so being added all the elements behind the elements to be moved backward position, remove elements, too, behind All the elements you want to move forward, so efficiency is very low additions of elements of the array.

3. Initialize the one-dimensional array, there are two ways:

(1) static initialization; (2) dynamic initialization.

 

package com.bjpowernode.java_learning;


public class D66_1_ {

  public static void main(String[] args) {

    // static initialization of a one-dimensional array of type int

    int[] a1 = {10,22,21};

    // get the first element 

    System.out.println ( "the first element:" A1 + [0 ]);

    System.out.println ( "last element:" A1 + [2 ]);

    System.out.println ( "last element:" + A1 [-a1.length. 1 ]);

    // number go 

    System.out.println ( "the number of elements in the array is:" + a1.length);

    // iterate one-dimensional array

    for(int i = 0;i<a1.length;i++) {

      System.out.println(a1[i]);

    }

    // the second element to 100 

    A1 [ . 1] = 100 ;

    System.out.println("===================");

    for(int i= 0;i<a1.length;i++) {

      System.out.println(a1[i]);

    }

  }


}

 

The above is the use of static initialization of a one-dimensional array initialization following a dynamic presentation dimensional array

 

    int[] a2 = new int[4];

    // reference type array 

    Object [] OBJS = new new Object [. 3 ];

    for(int index=0;index<objs.length;index++) {

      Object o = objs[index];

      // o.toString (); // Note null pointer exception 

      System.out.println (O); // null null null null pointer there is no abnormality, because pirintln

      // source inside this function to do this null pointer screening, you can see the source code

The following look at how to deal with this println source is null-pointer exceptions

 

Second, when to use dynamic initialization, when to use static initialization

1. Whether dynamic or static initialization initialization, the final memory layout is the same.

2. If at the time of creation of the array, the array should know what data is stored, of course, this time using a static initialization method. What if the data in the creation of the array, the array storage can not be predicted, but to open up space, use dynamic initialization mode.

The following two ways are possible initialization

an int 3a , [] = {12,12,45 };

an int [] = {3a , 12,12,45};

 

Fourth, the source code:

D66_ArryInitialMethods.java

https://github.com/ruigege66/Java/blob/master/D66_ArryInitialMethods.java2.CSDN:https://blog.csdn.net/weixin_44630050

3.博客园:https://www.cnblogs.com/ruigege0000/

4.欢迎关注微信公众号:傅里叶变换,个人公众号,仅用于学习交流,后台回复”礼包“,获取大数据学习资料

 

Guess you like

Origin www.cnblogs.com/ruigege0000/p/12110173.html