13--Array (overview, definition and use of one-dimensional array)

Overview of arrays

Array (the Array): , a plurality of the same type of data by the arrangement of a certain order of collection, using a name and by number for unified management of the data mode.
The array itself is a reference data type , and the elements in the array can be any data type (basic data type and reference data type).
Creating an array object will always open up a continuous space in the memory, and the array name refers to the first address of this continuous space. Once
the length of the array is defined, it cannot be changed .
We can directly call the element at the specified position by subscript (index).
Common concepts of arrays:

  1. Array name

  2. Subscript (or index)

  3. element

  4. The length of the
    array The classification of the array:

  5. Classified by dimensions: one-dimensional array, two-dimensional array, three-dimensional array...

  6. According to the classification of the element types in the array: arrays of basic data type elements (integer arrays, byte arrays, etc.) and arrays of reference data type elements (ie, object arrays)

  7. Classified by definition: dynamic type array, static type array

Use of one-dimensional arrays

The definition of a one-dimensional array:
int[] num or int num[];
Note: The length of the value (the number of elements in the array) cannot be specified when declaring an array in java, for example: int score[10]//error
one-dimensional Initialization of the array:

  1. Dynamic initialization: The operations of array declaration and allocating space for array elements and assignment are carried out separately.
    Example 1:
package com.qwy;

public class Demo01 {
    
    
	public static void main(String[] args) {
    
    
		//动态声明一维数组:声明,并初始化
		int[] score=new int[5];
		//给数组赋值
		score[0]=88;
		score[1]=99;
		score[2]=100;
		score[3]=85;
		score[4]=88;
		//动态声明一维数组:先声明,再初始化
		String names[];
		names=new String[3];
		//给数组赋值
		names[0]="张三";
		names[1]="李四";
		names[2]="王五";
		
	}
}

  1. Static initialization: When defining the array, allocate space and assign values ​​to the array elements.
    Example 2:
package com.qwy;

public class Demo02 {
    
    
	public static void main(String[] args) {
    
    
		//静态声明一维数组:声明,并赋值,注意不要编写数组长度
		int[] score=new int[]{
    
    88,89,99,100,86};

		//静态声明一维数组,直接赋值
		String names[]={
    
    "张三","李四","王五"};
		
		
	}
}

Reference of array elements:
1. After defining the use of new to allocate space for it, each element in the array can be referenced.
2. The reference method of array elements: array name [array element subscript]
1) The array element subscript can be an integer constant or a revitalizing expression: such as a[3], b[i], c[6*i];
2 ) Array element subscript starts from 0; the valid subscript value range of an array of length n: 0 —>n-1; such as int a[]=new int[3]; The array element that can be referenced is a[0] , A[1], a[2]
3. Each array has an attribute length indicating its length, for example: a.length indicates the length of the array a (number of elements)
Note: Once the array is initialized, its length is not allowed changing.
The default initialization value of the array elements: the
array is a reference type, and its elements are equivalent to the member variables of the class, so once the array is allocated space, each element in it is implicitly initialized in the same way as the member variables.
Example 3:

package com.qwy;

public class Demo03 {
    
    
	public static void main(String[] args) {
    
    
		//静态声明一维数组:声明,并赋值,注意不要编写数组长度
		int[] score=new int[5];

		System.out.println(score[0]);//默认值0
		System.out.println(score[2]);//默认值0
	}
}

  1. For arrays of basic data types, the default initial values ​​are the default values ​​of the corresponding basic data types.
    Reference: https://blog.csdn.net/qwy715229258163/article/details/113667381
  2. For arrays of application data types, the default initial values ​​are all null.

Memory map of one-dimensional array

The keyword new is used in java to create an array. Create a one-dimensional array of basic data type elements:
Example 4:

package com.qwy;

public class Demo04 {
    
    
	public static void main(String[] args) {
    
    
		//创建一维数组:基本数据类型数组在显式赋值之前,Java会自动给他们赋默认值。 
		int[] scores=new int[5];
	}
}

Memory graph: before explicit assignment
Insert picture description here
Example 5: after explicit assignment

package com.qwy;

public class Demo05 {
    
    
	public static void main(String[] args) {
    
    
		//创建一维数组:基本数据类型数组在显式赋值之前,Java会自动给他们赋默认值。 
		int[] scores=new int[5];
		//重新赋值
		scores[0]=88;
		scores[1]=99;
		scores[2]=100;
		scores[3]=85;
		scores[4]=88;
	}
}

Memory map:
Insert picture description here

Guess you like

Origin blog.csdn.net/qwy715229258163/article/details/114006001