# 05. Array

Array is a reference type, and array data is an object

After the array is created, if there is no assignment, there are default values: byte[0], short[0], int[0], long[0], float[0.0], double[0.0], char[\u0000], boolean[false], String[null]

### Array assignment mechanism

- Basic data type assignment. This value is specific data and does not affect each other (value copy).
- By default, the array is passed by reference, and the assigned value is the address (address copy).

Resolution:

- The memory of the JVM is mainly divided into three areas: stack, heap and method area.
- The data of basic data type is placed on the stack. For example, int a = 1 and int b = a open up two spaces in the stack to store the specific values of a and B, so changing B will not affect the value of A.
- After the array is created, for example, int[] arr1 = {...} and int[] arr1 = arr1 actually open up a space in the stack called arr1, which stores the address and points to the specific data actually stored, and these data are stored in the heap. When the value of arr1 is assigned to arr2, the address value is actually assigned to arr2, so arr2 also points to the same data in the heap, Therefore, the value of arr1 can also be changed through arr2. (that is, a total of three spaces are opened, two in the stack and one in the heap)

### Multidimensional array

int[][] arr = new int[2][3]; //arr[i] represents the address of row I of the two-dimensional array //arr.length indicates the width of the two-dimensional array, that is, the number of one-dimensional arrays //arr[i].length indicates the number of elements in each row of the two-dimensional array, that is, the length of each one-dimensional array

Resolution:

A space arr is opened in the stack, which stores the address and points to a space in the heap. This space also stores the address, that is, arr[0] and arr[1]. Then, arr[0] points to another space in the heap, which is the specific value of the first one-dimensional array in the two-dimensional array. Similarly, arr[1] also points to another space in the heap, which stores the specific value of the second two-dimensional array in the two-dimensional array. (that is, a total of four spaces are opened, one in the stack and three in the heap)

The number of columns of multi-dimensional arrays in Java can be uncertain, that is, the length of each one-dimensional array can be the same or different

Example: Yang Hui triangle

/* * 1 * 1 1 * 1 2 1 * 1 3 3 1 * 1 4 6 4 1 * 1 5 10 10 5 1 * *Idea: turn complexity into simplicity, die first and live later * * */ //First of all, regardless of the specific number, print out the shape of the triangle first. After observation, we can find that the number of layers of Yang Hui triangle is equal to the number of layers //Then, the number of layers of the triangle is also given a certain value, such as 6 layers. According to the uncertain number of columns of the multidimensional array, the for loop is used to create the array int[][] arr = new int[6][]; for(int i = 0; i < arr.length; i++){ arr[i] = new int[i + 1]; } //Of course, we can write an auxiliary for loop to traverse the array to see the current effect for(int i = 0; i < arr.length; i++){ for(int j = 0;j < arr[i].length; j++){ System.out.print(arr[i][j]); } System.out.println(); } /* * 0 * 00 * 000 * 0000 * 00000 * 000000 * * */ //At this time, the shape of Yang Hui triangle has been, and the number at each position is 0 by default //Then let's look for the rules and see what the specific values at each position have //It is found that both the first and second layers are 1. Starting from the third layer, the number on both sides is 1. The number at each other position is equal to the sum of the two nearest numbers on its upper layer (that is, the number right above and the number on its left) //Code representation: arr[i][j] = arr[i - 1][j] + arr[i - 1][j - 1] //So the corresponding specific figures are also available arr[0][0] = 1; arr[1][0] = 1; arr[1][1] = 1; for(int i = 2; i < arr.length; i++){ for(int j = 0; j <arr[i].length; j++){ if(j == 0 || j == arr[i].length - 1){ arr[i][j] = 1; }else { arr[i][j] = arr[i - 1][j] + arr[i - 1][j - 1]; } } } //Then print the array and Yang Hui triangle is completed /* * 1 * 11 * 121 * 1331 * 14641 * 15101051 * * */ //Then the "dead" part of the program will be "live", here is to set the number of layers of Yang Hui triangle to the value entered by the user //The complete code is as follows import java.util.Scanner; public class yanghui { public static void main(String[] args){ /* * 1 * 1 1 * 1 2 1 * 1 3 3 1 * 1 4 6 4 1 * 1 5 10 10 5 1 * *Idea: turn complexity into simplicity, die first and live later * * */ //The user inputs the number of layers of Yanghui triangle Scanner myScanner = new Scanner(System.in); System.out.println("Please enter the number of layers of Yang Hui triangle:"); int num = myScanner.nextInt(); //Create array int[][] arr = new int[num][]; for(int i = 0; i < arr.length; i++){ arr[i] = new int[i + 1]; } //Give Yang Hui triangle a specific value arr[0][0] = 1; arr[1][0] = 1; arr[1][1] = 1; for(int i = 2; i < arr.length; i++){ for(int j = 0; j <arr[i].length; j++){ if(j == 0 || j == arr[i].length - 1){ arr[i][j] = 1; }else { arr[i][j] = arr[i - 1][j] + arr[i - 1][j - 1]; } } } //Traversal array for(int i = 0; i < arr.length; i++){ for(int j = 0;j < arr[i].length; j++){ System.out.print(arr[i][j]); } System.out.println(); } } }

a key:

- You must not write the program directly as soon as you see the topic. Unless you have done this project or your level is much higher than this project, you will waste a lot of unnecessary time and even make mistakes.
- Be sure to do demand analysis first, that is, analyze ideas first, and then complete them step by step in order.
- When analyzing ideas, it is suggested to turn complexity into simplicity and live after death.

Turning complexity into simplicity is to divide complex problems into blocks. Some are similar to completing the basic modules first, and then adding new functions on this basis. For example, for the above topic, if you want to print Yang Hui triangle, you can't think of a method. It's better to start with the simplest. I'll print out the shape of the triangle first, and then give specific values. (there is also a classic topic: printing hollow triangles or hollow diamonds, as shown below)

To die first and live later is to "kill" some flexible and inconvenient problems, give them a specific value, and let them not hinder the completion of your basic modules. These problems are generally human factors that do not affect the operation of your program, such as the interaction with users. Like the number of layers in Yang Hui triangle, you first give them a certain value, When the function module is completed, it will be "activated".

//The drawings are drawn by the * trumpeter. They are not standard. Just understand the meaning /* * * * * * * * * * * * * * * * * * * * * * * * * * * * */ //If you want to print the hollow triangle directly, it's a little complicated. Let's print the solid triangle first /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ //It's a little complicated. Then remove the left side of the triangle, regardless of the left side /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ //We are very familiar with this figure, that is, printing triangles //As for the hollow diamond, it is further based on the hollow triangle //So this is the idea of hollow triangle. The code is as follows (the code of each step is not written in detail, just look at the total code) System.out.println("Please enter the number of rows:"); Scanner myScanner = new Scanner(System.in); int hang = myScanner.nextInt(); for(int i = 1; i <= hang; i++){ for(int k = 1; k <= hang - i; k++){ System.out.print(" "); } for(int j = 1; j <= i * 2 - 1; j++){ //System.out.print("*"); if(j == 1 || j == i * 2 - 1 || i == hang){ System.out.print("*"); } else { System.out.print(" "); } } System.out.println();

**Small suggestion: * * after completing each module, you can write an auxiliary method to display the effect of the code on the console, such as the method of traversing the array in Yang Hui triangle. You can traverse it every step to see what the array looks like, so as to facilitate you to understand the progress of completion, what problems have occurred, etc

Two dimensional arrays can be written in three forms:

int[][] a; int[] b[]; int c[][]; //Although not commonly used, this method is also correct, for example: String[] strs = new String[]{"a","b","c"}; //There is also a common method in the array: set an index int index = -1, which is generally used for loop judgment. If the index is not - 1, the operation is successful, and if it is still - 1, the operation fails or the target is not found.