0.IDEA development tools
0.1 common shortcut keys of idea
Shortcut key | function |
---|---|
Alt+Enter | Import package, automatically correct code |
Ctrl+Y | Delete the line where the cursor is located |
Ctrl+D | Copy the contents of the line where the cursor is located and insert it below the cursor position |
Ctrl+Alt+L | formatting code |
Ctrl+/ | Single-Line Comments |
Ctrl+Shift+/ | Select code comment, multiline comment, and then press cancel comment |
Alt+Shift + up and down arrows | Move current code line |
1. Array
1.1 what is an array [understanding]
An array is a container for storing data with a fixed length. The data types for storing multiple data should be consistent.
1.2 array definition format [memory]
1.2.1 type I
Data type [] array name
Example:
int[] arr; double[] arr; char[] arr;
1.2.2 second type
Data type array name []
Example:
int arr[]; double arr[]; char arr[];
1.3 array dynamic initialization [ application ]
1.3.1 what is dynamic initialization
Array dynamic initialization is to give only the length of the array and the default initialization value is given by the system
1.3.2 dynamic initialization format
data type[] Array name = new data type[Array length];
int[] arr = new int[3];
1.3.3 detailed explanation of dynamic initialization format
- To the left of the equal sign:
- int: data type of array
- []: represents that this is an array
- arr: represents the name of the array
- To the right of the equal sign:
- new: open up memory space for arrays
- int: data type of array
- []: represents that this is an array
- 5: Represents the length of the array
1.4 array element access [ application ]
1.4.1 what is an index
Each element stored in the array will automatically have a number, starting from 0.
This automatic numbering is called array index, and the elements in the array can be accessed through the index of the array.
1.4.2 access array element format
Array name[Indexes];
1.4.3 example code
public class ArrayDemo { public static void main(String[] args) { int[] arr = new int[3]; //Output array name System.out.println(arr); //[I@880ec60 //Output elements in array System.out.println(arr[0]); System.out.println(arr[1]); System.out.println(arr[2]); } }
1.5 memory allocation [understanding]
1.5.1 memory overview
Memory is an important original and temporary storage area in the computer. It is used to run programs.
The program we write is stored in the hard disk, and the program in the hard disk will not run.
It must be put into memory to run. After running, the memory will be emptied.
In order to run programs, Java virtual machine must allocate and manage memory space.
1.5.2 memory allocation in Java
- At present, we only need to remember two memories: stack memory and heap memory
Area name | effect |
---|---|
register | It has nothing to do with our development. |
Native Method Stack | JVM is used when using operating system functions, which has nothing to do with our development. |
Method area | Store class files that can be run. |
Heap memory | Storage objects or arrays, created by new, are stored in heap memory. |
Method stack | The memory used when the method runs, such as the main method, enters the method stack for execution. |
1.6 memory diagram of a single array [understanding]
1.7 memory diagram of multiple arrays [understanding]
1.8 multiple arrays point to the same memory map [understand]
1.9 array static initialization [ application ]
1.9.1 what is static initialization
When creating an array, the elements are directly determined
1.9.2 static initialization format
- Full version format
data type[] Array name = new data type[]{Element 1,Element 2,...};
- Simplified format
data type[] Array name = {Element 1,Element 2,...};
1.9.3 example code
public class ArrayDemo { public static void main(String[] args) { //Define array int[] arr = {1, 2, 3}; //Output array name System.out.println(arr); //Output elements in array System.out.println(arr[0]); System.out.println(arr[1]); System.out.println(arr[2]); } }
1.10 two common problems in array operation [ application ]
1.10.1 index out of bounds exception
- Cause of occurrence
public class ArrayDemo { public static void main(String[] args) { int[] arr = new int[3]; System.out.println(arr[3]); } }
The array length is 3 and the index range is 0 ~ 2, but we accessed an index of 3.
After the program runs, an array out of bounds exception of ArrayIndexOutOfBoundsException will be thrown. In the development, the out of bounds exception of the array cannot occur. Once it occurs, we must modify the code we write.
- Solution
Modify the wrong index to the correct index range!
1.10.2 null pointer exception
- Cause of occurrence
public class ArrayDemo { public static void main(String[] args) { int[] arr = new int[3]; //Assign null to array arr = null; System.out.println(arr[0]); } }
The line of code arr = null means that the variable arr will not save the memory address of the array, so it is not allowed to operate the array. Therefore, a null pointerexception null pointer exception will be thrown during operation. In the development, the out of bounds exception of the array cannot occur. Once it occurs, we must modify the code we write.
- Solution
Give the array a real heap memory space reference!
1.11 array traversal [ application ]
- Array traversal: it is to get each element in the array separately, which is traversal. Traversal is also the cornerstone of array operations.
public class ArrayTest01 { public static void main(String[] args) { int[] arr = { 1, 2, 3, 4, 5 }; System.out.println(arr[0]); System.out.println(arr[1]); System.out.println(arr[2]); System.out.println(arr[3]); System.out.println(arr[4]); } }
The above code can traverse all the elements in the array, but if there are many array elements, this writing method will certainly not work, so we need to transform it into a circular writing method. The index of the array is 0 to lenght-1, which can appear as a condition of the loop.
public class ArrayTest01 { public static void main(String[] args) { //Define array int[] arr = {11, 22, 33, 44, 55}; //Use a common traversal format for(int x=0; x<arr.length; x++) { System.out.println(arr[x]); } } }
1.12 array maximum [application]
- Get maximum value: find the maximum value from all elements of the array.
- Implementation idea:
- Define variables to hold the elements on the index of array 0
- Traverse the array to get each element in the array
- Compare the traversed element with the variable that holds the value on the index of array 0
- If the value of the array element is greater than the value of the variable, the variable records the new value
- After the array loop is traversed, the variable saves the maximum value in the array
- Code implementation:
public class ArrayTest02 { public static void main(String[] args) { //Define array int[] arr = {12, 45, 98, 73, 60}; //Defines the maximum value of a variable //Take the first data in the array as the initial value of the variable int max = arr[0]; //Compare with the remaining data in the array one by one, and save the maximum value to the variable each time for(int x=1; x<arr.length; x++) { if(arr[x] > max) { max = arr[x]; } } //Print the value of the variable at the end of the cycle System.out.println("max:" + max); } }