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
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.
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};
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:
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
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
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
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;i5) System.out.println(array[i]); }
Output result:
10 8 9 6 8 71 9 99
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
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]);
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:
The principle of using multiple arrays and single array memory is the same, so I won’t go into details here.
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]);
Output result:
[I@15db9742
100
200
300
[I@ 15db9742
111
[I@15db9742
111
Principle explanation:
//静态初始化数组 int [] array= {1,2,3}; System.out.println(array[3]);
//动态初始化数组 int [] array= new int[3]; array=null; System.out.println(array[0]);
Data type | Specific definition type | Default value |
Basic type | ##byte, short, char, int, long | 0|
float,double | 0.0||
boolean | #false||
Class, interface, array, String | null
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!