Two ways to initialize Java arrays that tens of thousands of Huawei employees want to know

This article describes two ways to initialize an array of Java, to help you better understand and learn to use Java, and interested friends can understand the next
one, array
- 1. array type storage elements are unified, each The space occupied by the elements in the memory is the same. Know the memory address of the first element of the array. As long as the element to be searched knows the subscript, the offset can be quickly calculated by adding the offset to the memory address of the first element You can quickly calculate the memory address of the element you want to find. The element is quickly located by the memory address, so the efficiency of the array to find the element is higher.

-2. Randomly add and delete elements to the array. When adding elements, in order to ensure that the elements in the array are in order in terms of spatial storage, all elements after the position of the added element must be moved backward, and the same is true for deleting elements. All subsequent elements need to be moved forward, so the addition and deletion of elements in the array is very inefficient.

-3.​There are two ways to initialize a one-dimensional array:

(1)​Static initialization;

(2) Dynamic initialization​.

package com.bjpowernode.java_learning;
 
 
public class D66_1_ {
    
    
 
 public static void main(String[] args) {
    
    
 
 //静态初始化一个int类型的一维数组
 
 int[] a1 = {
    
    10,22,21};
 
 //取得第一个元素
 
 System.out.println("第一个元素:" + a1[0]);
 
 System.out.println("最后一个元素:" + a1[2]);
 
 System.out.println("最后一个元素:" + a1[a1.length-1]);
 
 //去的个数
 
 System.out.println("数组中的的元素个数为:"+a1.length);
 
 //遍历一维数组
 
 for(int i = 0;i<a1.length;i++) {
    
    
 
  System.out.println(a1[i]);
 
 }
 
 //将第二个元素改为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, the following demonstrates the dynamic initialization of a one-dimensional array

int[] a2 = new int[4];
 
 //引用类型的数组
 
 Object[] objs = new Object[3];
 
 for(int index=0;index<objs.length;index++) {
    
    
 
  Object o = objs[index];
 
  //o.toString();//注意空指针异常
 
  System.out.println(o);//null null null这里就没有出现空指针异常,这是因为pirintln
 
  //这个函数的源码里面对这种空指针做了筛选,可以见源码
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210328162312209.png)

**下面看一下println的源码是如何处理这种空指针异常的**
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210328162336115.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2RjajE5OTgwODA1,size_16,color_FFFFFF,t_70)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210328162343530.png)




**二、什么时候使用动态初始化,什么时候使用静态初始化**
**1.无论是动态初始化还是静态初始化,最终的内存分布都是一样的。**

**2.如果在创建数组的时候,知道数组中应该存储什么数据,这个时候当然采用静态初始​化方式。如果在创建数组的时候,无法预测到数组中存储什么数据,只是先开辟空间,​则使用动态初始化方式。**

**以下两种初始化方式都是可以的**
``c
int a3[] = {
    
    12,12,45};
 
int [] a3 = {
    
    12,12,45};

3. Source code:
D66_ArryInitialMethods.java

https://github.com/ruigege66/Java/blob/master/D66_ArryInitialMethods.java

The above is the details of the two initialization methods of Java arrays. For more information about Java array initialization, please edit other related articles by Guan Xiao, or add wx1411943564 remarks (csdn) for more information

Guess you like

Origin blog.csdn.net/dcj19980805/article/details/115282641