Today, I start learning java data structure. Data structure, therefore, is a storage method of data.
First, open the simplest data structure, array learning.
array
1. Understanding of array: an array is a collection of multiple data of the same type arranged in a certain order and named with a name,
These data are uniformly managed by numbering.
2. Array related concepts:
Array name, element, subscript, subscript, index, length of array: number of elements
3. Array features:
Arrays are ordered
4. The array is a variable of reference data type. The element of an array can be either a basic data type or a reference data type
5. Creating an array object will open up a whole block of continuous space in memory
6. Once the length of the array is determined, it cannot be modified.
7. Array classification:
① Illumination dimension: one-dimensional array, two-dimensional array...
② According to the type of array elements: array of basic data type elements and array of reference data type elements
One dimensional array
1. Declaration and initialization of one-dimensional array
The right way:
int num;//statement num = 10;//initialization int id = 1001;//Declaration + initialization int[] ids;//statement //1.1 static initialization: array initialization and array element assignment are performed simultaneously ids = new int[]{1001,1002,1003,1004}; //1.2 dynamic initialization: array initialization and array element assignment are performed separately String[] names = new String[5]; int[] arr4 = {1,2,3,4,5};//Type inference
Wrong way:
`// int[] arr1 = new int[];` `// int[5] arr2 = new int[5];` `// int[] arr3 = new //int[3]{1,2,3};`
2. Reference of one-dimensional array elements: called through corner markers.
//The subscript (or index) of the array starts from 0 and ends with the length of the array - 1. names[0] = "Wang Ming"; names[1] = "Wang He"; names[2] = "placed under house arrest"; names[3] = "Sun Julong"; names[4] = "Wang Hongzhi";//charAt(0)
3. Array attribute: length
System.out.println(names.length);//5 System.out.println(ids.length);
explain:
Once the array is initialized, its length is determined. arr.length
Once the array length is determined, it cannot be modified.
4. Traversal of one-dimensional array
for(int i = 0;i < names.length;i++){ System.out.println(names[i]); }
5. Default initialization value of one-dimensional array element
Array element is an integer: 0
Array elements are floating point: 0.0
Array elements are char type: 0 or '\ u0000', not '0'
Array elements are boolean: false
Array element is a reference data type: null
6. Memory parsing of one-dimensional array
When declaring variables, the declared variables will be stored in the stack, and the created array will appear in the heap space. The variables in the stack store the address value of the first element of the array.
Then perform the default initialization of data and subsequent assignment operations.
Two dimensional array
1. How to understand two-dimensional array?
Array is a reference data type
The elements of an array can also be reference data types
If the elements of a one-dimensional array a are of one-dimensional array type, the array A is called a two-dimensional array.
2. Declaration and initialization of two-dimensional array
The right way:
int[] arr = new int[]{1,2,3};//One dimensional array //initiate static int[][] arr1 = new int[][]{{1,2,3},{4,5},{6,7,8}}; //Dynamic initialization 1 String[][] arr2 = new String[3][2]; //Dynamic initialization 2 String[][] arr3 = new String[3][]; //It is also the correct way to write: int[] arr4[] = new int[][]{{1,2,3},{4,5,9,10},{6,7,8}}; int[] arr5[] = {{1,2,3},{4,5},{6,7,8}};//Type inference
Wrong way:
`// String[][] arr4 = new String[][4];` `// String[4][3] arr5 = new String[][];` `// int[][] arr6 = new int[4][3]{{1,2,3},{4,5},{6,7,8}};`
3. How to call 2D array elements:
System.out.println(arr1[0][1]);//2 System.out.println(arr2[1][1]);//null arr3[1] = new String[4]; System.out.println(arr3[1][0]); System.out.println(arr3[0]);//
4. Properties of two-dimensional array:
``System.out.println(arr4.length);//3` `System.out.println(arr4[0].length);//3` System.out.println(arr4[1].length);//4`
5. Traverse two-dimensional array elements
for(int i = 0;i < arr4.length;i++){ for(int j = 0;j < arr4[i].length;j++){ System.out.print(arr4[i][j] + " "); } System.out.println(); }
6. Default initialization value of two-dimensional array elements
Regulation: the two-dimensional array is divided into the elements of the outer array and the elements of the inner array
int[][] arr = new int[4][3];
Outer elements: arr[0],arr[1], etc
Inner elements: arr[0][0],arr[1][2], etc
⑤ Default initialization value for array elements
For initialization method 1: for example:
int[][] arr = new int[4][3];
The initialization value of the outer element is: address value
The initialization value of the inner element is: the same as that of the one-dimensional array
For initialization mode 2: for example:
int[][] arr = new int[4][];
The initialization value of the outer element is null
The initialization value of the inner element is: it cannot be called, otherwise an error will be reported.