1. Preface
Overview of learning: In the first eight days, we learned the basics of grammar, operators and expressions, loop structures, and branch structures. Today we mainly learn the definition of arrays, related attribute methods, and array storage. Memory diagram, common mistakes
Learning goals: Master the two definition methods of arrays, related properties, understand memory principles, error resolution
2. Definition of arrays
1 .Overview
If the grades of a classmate need to be stored, what method should be used?
As we learned before, multiple variables can be defined to store different scores. But if there are more than 1,000 students, then how about defining more than 1,000 variables? Of course not, this requires using our array.
2. Static initialization of array
Features: Directly assign values to the array when defining it, and the system determines the array length
General format:
Data type [] array name = { element 1, element 2, element 3,... };
For example:
int [] array= {1,2,3,4,5};
double[] scores = {88.5, 99.5, 59.5};
3. Dynamically initialize the array
Features: Determine the type of the element and the length of the array when defining the array, and then Storing data
General format:
Data type[]Array name=new Data type[length];
For example:
int [] array= new int [5];
double[] scores = new double[3];
Default value:
Data type | Specific definition type |
Default value |
Basic type |
##byte, short, char, int, long
| 0 |
float,double
| 0.0 |
boolean
| #false |
Reference type |
Class, interface, array, String
| null |
4. Summary
Arrays are suitable for large amounts of data of the same type
Static initialization is suitable for knowing the element value
Dynamic initialization is suitable for unclear data to be stored
3. Array attributes
1. Access
General access The format of the array is:
array name[index]
Example question:
//静态初始化数组
int [] array= {1,2,3,4,5};
System.out.println(array[0]);//输出 1
System.out.println(array[1]);//输出 2
System.out.println(array[3]);//输出 4
Copy after login
2. Length
The length can be called directly length gets the length of the array.
Example question:
//静态初始化数组
int [] array= {1,2,3,4,5};
System.out.println(array.length);//调用方法,输出长度 5
//最大索引array.length-1
Copy after login
3. Traversal
Traversal is accessing array elements one by one, mainly used in search, data statistics...
We have learned about loop structures and branch structures before. Let’s traverse an array through a for loop
Example question:
Given elements {10,8,9,4,5,6,8 ,71,2,3,9,99}, use a static array to store and output elements greater than 5 in the array?
Encoding implementation:
//静态初始化数组
int [] array= {10,8,9,4,5,6,8,71,2,3,9,99};
for(int i=0;i<array.length;i++)
{
if(array[i]>5)
System.out.println(array[i]);
}
Copy after login
Output result:
10 8 9 6 8 71 9 99
IV. Memory map
When Java runs the program, it needs to allocate space in the memory and divide the space into different areas.
Stack memory: stores local variables and disappears immediately after use
Heap memory: stores the contents (objects, entities) and addresses of new After use, recycle when the garbage collector is idle
1. Single array memory graph
The following code to create an array implements its memory relationship graph
Encoding implementation:
//动态初始化数组
int [] arr=new int[3];
System.out.println(arr);
System.out.println(arr[0]);
System.out.println(arr[1]);
System.out.println(arr[2]);
//修改值
arr[0]=100;
arr[2]=200;
System.out.println(arr);
System.out.println(arr[0]);
System.out.println(arr[1]);
System.out.println(arr[2]);
Copy after login
Output result:
[I@15db9742
0
0
0
[I@15db9742
100
0
200
Principle explanation:
Dynamic initialization first generates a new in the heap memory An arr address value, depending on the results of the compiler, assume 001 here. Due to dynamic initialization, each element has an initial value. For details, see the table above. When we output elements, we first access the array name address, go to the heap memory subscript, and then output the element value.
To modify the array value, the process is the same as viewing it, except that there is one more step in the modification process, as shown below:
2. Multiple array memory diagram
The principle of using multiple arrays and single array memory is the same, so I won’t go into details here.
3. The arrays point to the same memory
If we change the address values of the two arrays to be the same, what will the modified result be like, as shown in the following code.
Encoding implementation:
//动态初始化数组
int [] arr=new int[3];
arr[0]=100;
arr[1]=200;
arr[2]=300;
System.out.println(arr);
System.out.println(arr[0]);
System.out.println(arr[1]);
System.out.println(arr[2]);
int [] arr2=arr;
arr2[0]=111;
arr2[1]=222;
arr2[2]=333;
System.out.println(arr);
System.out.println(arr[0]);
System.out.println(arr2);
System.out.println(arr2[0]);
Copy after login
Output result:
[I@15db9742
100
200
300
[I@ 15db9742
111
[I@15db9742
111
Principle explanation:
##The first array is in the heap memory The address of is 001, and the second array is also 001, so modifying the value of the second array is actually the same array memory. The value of the first array will also change accordingly, and the result is as follows: 5. Frequently Asked Questions1. Index out of bounds//静态初始化数组
int [] array= {1,2,3};
System.out.println(array[3]);
Copy after login
- After the above code is run, the following error exception will appear:
- Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
- Explanation: We statically initialized the array and gave it 3 numbers. The maximum index is 2. When we access 3, an error will be reported.
2. Null pointer Exception //动态初始化数组
int [] array= new int[3];
array=null;
System.out.println(array[0]);
Copy after login
- After the above code is run, the following error exception will appear:
- Exception in thread "main" java.lang.NullPointerException
- Explanation: We set the array to null, causing the array accessed not to point to the data in the heap memory
The above is the detailed content of Example analysis of how to use Java arrays. For more information, please follow other related articles on the PHP Chinese website!