array
1. Array concept
Array: A fixed container for storing multiple elements of the same data type in a program.
Prerequisites for arrays (characteristics):
1. Once the array is initialized, its length is fixed;
2. The elements in an array must be of the same data type in memory;
3. The number of elements in the array must be multiple;
2. Array declaration initialization
Array declaration:
data type[] Array name;(Recommend) Data type array name[];
Initialization of the array:
dynamic initialization:Initializing arrays only initializes the length of the array,The specific data values in the array will not be initialized,JVM You can get the length of the array directly
Format:
data type[] Array name = new data type[Array length];
Static Initialization: Initializing the array only initializes the specific data values inside the array, does not initialize the length of the array, JVM can obtain the length of the array indirectly
Standard format:
data type[] Array name = new data type[]{Element 1,Element 2,......,element n};
Simplified format:
data type[] Data Name = {Element 1,Element 2,......,element n};
Noun Interpretation for Array Declaration Initialization:
Data type: The data type of the element in the array
[]: declares a one-dimensional array
Data type []: The data type of the array
Array name: the name of the array
new: Request and open up appropriate memory space
Array length: how many elements can be stored in an array
Considerations for initializing array declarations:
1. Dynamic and static initialization of arrays are not recommended. You need to decide which initialization method to use based on your actual needs.
2. Declarative initialization of arrays by static and dynamic combination is not allowed in programs
3. Elements in arrays support variable type conversion
4. Simplified format for static initialization of arrays does not allow declaration before initialization
//Array declaration 1: data type [] array name; int[] arr01; //Array declaration 2: data type array name []; int arr02[]; //Initialization of Arrays 1: Dynamic Initialization int[] arr03 = new int[3]; //Array Initialization 2: Static Initialization Standard Format int[] arr04 = new int[]{11,22,33}; //Initialization of Arrays 3: Static Initialization Simplified Format int[] arr05 = {11,22,33}; //Initialization via a combination of static and dynamic arrays: Error //int[] arr06 = new int[5]{11,22,33}; error //Type conversion of elements can be supported in arrays int[] arr07 = {11,22,33,'a',(int)3.14}; //Dynamic Initialization: Declare then Initialize int[] arr08; arr08 = new int[3]; //Static Initialization: Standard Format Declares and Initializes int[] arr09; arr09 = new int[]{11,22,33}; //Static Initialization: Simplified format does not allow declaration before initialization int[] arr10;
3. Use of arrays
Get the elements in the array
Get the length of the array
Gets the elements in the array:
Need to get through Index Value
Index value: Number of numbers according to certain rules for elements in an array
Rule for indexed values: Element numbers start at 0 and end at length-1 of the array. No indexed values exist for arrays of length 0
Format:
Array name[Index Value]
Get the length of the array
Array name.length
Dynamic Access Array Length
All arrays provide a length property through which the length of the array can be dynamically accessed, and once the length of the array is obtained, each element of the array can be iterated through in a loop.
The code is as follows:
//Create arrays through dynamic initialization int[] arr = new int[3]; System.out.println(arr); System.out.println(arr[0]); System.out.println(arr[1]); System.out.println(arr[2]); System.out.println("=========================="); arr[0] = 11; arr[1] = 22; arr[2] = 33; System.out.println(arr); System.out.println(arr[0]); System.out.println(arr[1]); System.out.println(arr[2]);
Static Access Array Length
//Create arrays by static initialization int[] arr = {11,22,33}; System.out.println(arr); System.out.println(arr[0]); System.out.println(arr[1]); System.out.println(arr[2]);
Three exceptions in the array:
1.NegativeArraySizeException exception: negative array length exception
Cause: negative length when creating arrays dynamically
2.ArrayIndexOutOfBoundsException: Array index out of bounds exception
Cause: Access error or nonexistent index
3.NullPointerException exception: null pointer exception
Cause: Get array elements or object members by null
4. Use of arrays
1. Print arrays in a fixed format
int[] arr = {11,22,33,44}; for (int i = 0; i < arr.length ;i++ ) { System.out.println(arr[i]); } System.out.println("==========================="); //Array: [11, 22, 33, 44] System.out.print("array:["); for (int i = 0; i < arr.length ;i++ ) { if (i == arr.length - 1) { //Determine if index is final System.out.println(arr[i] + "]"); } else { System.out.print(arr[i] + ", "); } }
2. Sum of Arrays
// Define an array int[] arr = { 11, 22, 33, 44, 55 }; // Define summation traversal int sum = 0; // foreach for (int i = 0; i < arr.length; i++) { // The cumulative sum for each element in the array sum += arr[i]; } System.out.println("sum:" + sum);
3. Array Get Maximum Element
- Maximum Get: Find the maximum value from all elements of the array.
- Ideas for implementation:
1. Define variables to hold elements on array 0 index
2. Traverse the array to get each element in the array
3. Compare traversed elements with variables that hold values on the 0 index of the array
4. If the value of an array element is greater than the value of a variable, the variable records the new value
5. At the end of the loop, the variable holds the maximum value in the array
int[] arr = { 5, 15, 2000, 10000, 100, 4000 }; //Define the variable to hold the element of the 0 index in the array int max = arr[0]; //Traverse through the array, taking out each element for (int i = 0; i < arr.length; i++) { //Comparison of traversed elements and variables max //If the array element is greater than max if (arr[i] > max) { //max records large values max = arr[i]; } } System.out.println("The maximum value of the array is:" + max);