catalogue
5 use of one-dimensional array:
① Declaration and initialization of one-dimensional array
② How to call the specified position element of an array
③ How to get the length of an array
④ How to traverse array elements
⑤ Default initialization value for array elements
Understanding stacks and heaps
Relationship between stack and heap
array
1. Understanding of arrays:
An Array is a collection of multiple data of the same type arranged in a certain order and named with a name,
* These data are uniformly managed by numbering.
2. Array related concepts:
> Array name
> element
> Len gt h of array
> Index, subscript, angle
3. Array features:
1) Arrays are ordered
2) Array is a variable of reference data type. The elements of the array can be either basic data type or reference data type
There are three types of reference data: class, array and interface
3) Creating an array object opens up a whole contiguous space in memory
4) Once the length of the array is determined, it cannot be modified
4. Classification of arrays:
① According to dimension: one-dimensional array, two-dimensional array
② According to the data type of the element: array of basic data type elements and array of reference data type elements
5 use of one-dimensional array:
① Declaration and initialization of one-dimensional array
int num;//statement num = 10;//initialization int id = 1001;//Declaration + initialization int[] ids;//statement //1.1 static initialization: array initialization and array element assignment are performed simultaneously ids = new int[] {1001,1002,1003,1004}; //1.2 dynamic initialization: array initialization and array element assignment are performed separately String[] names = new String[4]; //Once the array is initialized, its length is determined
② How to call the specified position element of an array
//The subscript starts at 0 and ends at the length - 1 of the array names[0] = "Hai Yu Chang"; names[1] = "Zhang San"; names[2] = "Li Si"; names[3] = "Wang Wu";
③ How to get the length of an array
//With the help of the attribute of array: length System.out.println(names.length);//4 System.out.println(ids.length);//4
④ How to traverse array elements
//Traversal over and over again System.out.println(names[0]); System.out.println(names[1]); System.out.println(names[2]); System.out.println(names[3]); //When the number of array elements is large, the above method is obviously problematic, so we think of the loop structure for(int i = 0;i < names.length;i ++) { System.out.println(names[i]); }
⑤ Default initialization value for array elements
Array elements have basic data types and reference data types. Their default values cannot be the same
* Array elements are integer. The default initialization value is: 0
int[] arr = new int[4]; for(int i=0;i<arr.length;i++) { System.out.println(arr[i]); }
* Array elements are floating-point. The default initialization value is: 0.0
double[] arr1 = new double[4]; for(int i=0;i<arr1.length;i++) { System.out.println(arr1[i]); }
* Array elements are of char type. The default initialization value is 0 or '\ u0000', instead of '0'. It looks like a space effect
char[] arr2 = new char[4]; for(int i=0;i<arr2.length;i++) { System.out.println(arr2[i]); } //Check whether the initialization value is 0 if (arr2[3] == 0) { System.out.println("Hello, Haiyu!"); }
* Array elements are of boolean type. The default initialization value is: false
boolean[] arr3 = new boolean[4]; for(int i=0;i<arr3.length;i++) { System.out.println(arr3[i]); }
* The array element is a reference data type. The default initialization value is: null
String[] arr4 = new String[4]; System.out.println(arr4[2]); //Check whether the initialization value is null if(arr4[2] == null) { System.out.println("low-key"); }
Understanding stacks and heaps
Stack
Stack, also known as stack, is a linear table with limited operation. Limit linear tables to insert and delete only at the end of the table. This end is called the top of the stack, and the other end is called the bottom of the stack. Inserting a new element into a stack is also called stack entry, stack entry or stack pressing. It puts the new element above the top element of the stack to make it a new top element; Deleting an element from a stack is also called out of stack or out of stack. It deletes the top element of the stack and makes its adjacent elements become new top elements.
heap
Heap is computer science A general term for a special class of data structures in. A heap is usually a tree that can be seen as a tree Complete binary tree Array object for.
Relationship between stack and heap
The variables we are learning now (the ones from int) are local variables