Java basic learning (two) array

1 Array definition

数组的概念
It is a container that can store multiple data values ​​at the same time.
数组的特点

  1. Array is a reference data type
  2. Multiple data in the array must be of the same type
  3. The length of the array cannot be changed during the running of the program

2 Define the format

2.1 Dynamic formatting (specify length)

Format: data type [] array name = new data type [array length];
analytic meaning: the data type on the left is the data stored in the array, all of which are uniform.
The brackets on the left: it means I am an array.
Array name on the left: Give the array a name.
New on the right: represents the action of creating an array. .
Data type on the right: It must be consistent with the data type on the left.
The length of the brackets on the right: that is, how many data can be stored in the array, which is an int number.

public static void main(String[] args) {
    
    
       // 创建一个数组,里面可以存放300个int数据
       // 格式:数据类型[] 数组名称 = new 数据类型[数组长度];
       int[] arrayA = new int[300];

       // 创建一个数组,能存放10个double类型的数据
       double[] arrayB = new double[10];

       // 创建一个数组,能存放5个字符串
       String[] arrayC = new String[5];
   }

2.2 Static initialization

When creating an array, do not directly specify the number of data, but directly specify the specific data content.

Static initialization basic/standard format:
data type [] array name = new data type [] {element 1, element 2, …}

Format omitted:
data type [] array name = {element 1, element 2, …}

Precautions:

  1. Although static initialization does not directly tell the length, the length can also be automatically calculated based on the specific content of the elements in the braces.
  2. The standard format of static initialization can be split into two steps.
  3. Dynamic initialization can also be split into two steps.
  4. Once static initialization uses the omitted format, it cannot be split into two steps.

Usage suggestion:
If you are not sure of the specific content in the array, use dynamic is the truth, otherwise, if the specific content has been determined, use static initialization.

public static void main(String[] args) {
    
    
	// 直接创建一个数组,里面装的全都是int数字,具体为:5,15,25
	int[] arrayA = {
    
    5, 15, 25};
	
	// 静态初始化的标准格式,可以拆分为两个步骤
	int[] arrayB;
	arrayB = new int[]{
    
    1, 2, 3};
	
	// 动态初始化也可以拆分为两个步骤
	int[] arrayC;
	arrayC = new int[5];
	
	// 静态格式化一旦使用省略格式,就不能拆分为两个步骤
	// int[] arrayD;
	// arrayD = {1, 2, 3};
}

3 Access array elements to get

Print the array name directly, and get the corresponding array: the hash value of the memory address

The format for accessing array elements: array name [index value]
index value: is an int number, representing the number of elements in the array.
[Note] The index value starts from 0 and ends at "array length is -1".

public static void main(String[] args) {
    
    
	// 静态初始化的省略格式
	int[] array = {
    
    10, 20, 30};
	
	// 直接打印数组当中的元素
	System.out.println(array);      // [I@1b6d3586
	System.out.println(array[0]);   // 10
	
	// 也可以将数组当中的某一个单个元素,赋值交给变量
	int num = array[1];
	System.out.println(num);
}

4 Access array elements for assignment

When using a dynamically initialized array, its elements will automatically have a default value. The rules are as follows:

Guess you like

Origin blog.csdn.net/zx77588023/article/details/114108085