day04 summary notes
01 array introduction
- Introduction: array is a kind of container, which can store multiple values of the same data type
int[] arr = {10,20,'a'}; System.out.println(arr[2]); // 97 double arr = {10,20,30}; System.out.println(arr[0]); // 10.0 ----------------------------------------------- proposal: The same data type is stored in the array
Question: when to use arrays?
- If there are multiple data to be operated, and multiple data belong to the same group of data, you can consider using array container
02 - definition format of array
1. data type[] Array name; int[] arr; 2. Data type array name[]; double arr[];
- Note: this definition format only defines variables of array type, and the container has not opened up space in memory
03 - static initialization of array
- Initialization: the process of opening up space for array containers in memory and storing data into space
- Static initialization:
1. Full format: data type[] Array name = new data type[]{Element 1, Element 2, Element 3...}; int[] arr = new int[3]{11,22,33}; 2. Simplified format: data type[] Array name = {Element 1, Element 2, Element 3...}; int[] arr = {11,22,33}; be careful: Print array name, See the hexadecimal memory address of the array [I@233ac4 @ : Separator [ : The current space is an array type, Several brackets, It's a few dimensional array. I : Type of container 233ac4 : Hexadecimal address
04 - element access of array
- Format: array name [index];
- Index | subscript | subscript: the number corresponding to the space in the array. The number starts from 0 and increases by + 1 one by one
int[] arr = {11,22,33,44,55}; System.out.pritnln(arr[0]); // Print int result = arr[1] + 100; // calculation if(arr[2] % 2 == 0){ // judge } arr[3] = 66; // modify for(int i = 1; i <= arr[4]; i++){ // loop System.out.println("itheima"); }
05 - traversal of array
- Introduction: take out each element in the array
- Traversal usage scenario: if you want to implement the requirements of the array, you need to traverse the array to operate on [Each] element in the array
for(int i = 0; i < arr.length; i++){ // i: index // arr[i]: element } Array name.length : Get the length of the array dynamically (Number of elements)
06 - dynamic initialization of array
- Introduction: when initializing the array, you only need to manually specify the length, and the system will allocate the default value
- Format: data type [] array name = new data type [length];
int[] arr = new int[3]; System.out.println(arr[0]); // 0
- Classification of default values:
integer: 0 decimal: 0.0 Boolean: false character: '\u0000' ---> Unicode character ----> Common white space characters Reference data type : null -------------------------- Reference data type: array, class, Interface null(Null value) : Can only be assigned to a reference data type
Differences between the two initialization methods:
- Static initialization: specify elements manually, and the system will automatically calculate the length according to the number of elements
- Dynamic initialization: specify the length manually, and the system will assign the default value
There are two usage scenarios for initialization:
- Static initialization: if the data to be operated is specified in the requirements
demand: The grade of students in the known class is 100 20 100 30 100 int[] arr = {100,20,100,30,100};
- Dynamic initialization: I only know how many numbers to save, but I don't know what it is
demand: Enter five integers on the keyboard, Find the maximum value demand: Generate 10 1~100 Random number between, Find the minimum value
07 - memory diagram of array
- Method area: the memory entered when the bytecode file (. class) is loaded
- Stack: the memory entered when the method is running
- Heap: the contents of new will enter the heap memory, and usually open up space to generate addresses
The following memory needs no attention
- Local method stack:
- Register:
requirement: Be sure to open it ppt, Sort out today's memory map.
08 array FAQ
ArrayIndexOufOfBoundsException: array index out of bounds exception
- Reason: a nonexistent index was accessed
NullPointerException: null pointer exception
- Reason: when the data type variable is referenced and null is recorded, the connection with heap memory will be cut off
- If you still want to access heap memory data at this time, a null pointer exception will appear
- Objective: when you see these two errors in the future, you can find the location of the code error and solve the code error according to the error prompt
09 - Introduction to two-dimensional array
Introduction: two dimensional array is also a kind of container, which stores one-dimensional array
- Understanding: nesting of arrays
Usage scenario of two-dimensional array:
- There are multiple groups of data to be operated
- It is recommended to use multiple two-dimensional arrays for overall maintenance
10 - static initialization of two-dimensional array
- Static initialization format:
data type[][] Array name = new data type[][]{ {One dimensional array 1}, {One dimensional array 2} }; data type[][] Array name = { {One dimensional array 1}, {One dimensional array 2} }; --------------------------------------- int[][] arr = { {11,22,33}, {44,55,66} };
- Element access of two-dimensional array:
Array name[m Indexes][n Indexes]; m Indexes : Which one-dimensional array do you want to access n Indexes : Which element of a one-dimensional array do you want to access System.out.println(arr[1][0]); // 44
11 - two dimensional array traversal
Idea:
- Traverse the two-dimensional array and get each one-dimensional array
- Continue to traverse the one-dimensional array to get the specific elements
- code:
for(int i = 0; i < arr.length; i++){ // arr[i]: every one-dimensional array for(int j = 0; j < arr[i].length; j++){ System.out.println(arr[i][j]); } }
12 - dynamic initialization of two-dimensional array
- Format:
data type[][] Array name = new data type[m][n]; m : How many one-dimensional arrays can be stored n : Each one-dimensional array, How many elements can be stored int[][] arr = new int[3][1]; A two-dimensional array can store three one-dimensional arrays, Each one-dimensional array can store one element.
13 - two dimensional array memory diagram
- Be sure to open the ppt and sort out the internal structure