An
overview of arrays and the format of the definitions
The function of the array:
- Multiple values used to store the same data type
Basic concepts of arrays:
- An array is a collection of multiple elements that store the same data type. is equivalent to a container.
Note: An array can store either the base data type or the reference data type. The format of the array definition:
- data type [] Array name = new data type [specify length of array]
initialization of the array
Concept:
is to open up contiguous memory space for arrays and assign values to the elements of each number
How to initialize:
- Dynamic initialization: Specifies the length of the system given initialization value int[] arr = new Int[5]
- Static initialization: gives the initialized value
- Format: data type [] Array name = new data type [array length]
If you print an array, you'll find something very interesting.
int New int [5]; System.out.println (arr)// Result: "[email protected] a hexadecimal memory address
[[Email protected] Explanation:
- "The representation is an array, and a few of them represent a few dimensions.
- I represents the int type
- @ A fixed notation
- The hexadecimal number of that string is the memory address where the array resides.
assigning and taking values of arrays
Arrays are assigned and evaluated by subscript, index
When we use dynamic initialization, the system defaults to a default value:
- Integer type: Bytes,short,int,long The default initialization value is 0
- Floating-point type: The default initialization value for Float,double is 0.0
- Boolean type: Boolean default initialization value is False
allocation of memory in Java
In Java, we have divided the memory into 5 parts:
- Stacks: Parts that store local variables
- Heap: Part of the object that stores new
- Method Area: A method in a class that wants to invoke a method if the method is in the method area
- Local method area:?
- Registers: For CPU use
Let's go to create an array:
int New int [5]
In fact, the inside of the computer will place the variable name arr in the stack area, new int[5] in the heap area, open up 5 contiguous space in the heap area, the default int is 0
When we re-assign arr to a new variable, it just gives the new variable the memory address.
static initialization of arrays
Dynamic initialization: int[] arr = new Int[5]
Static initialization:
Format: data type [] Array name = new data type []{element ...}
Simplification: data type [] Array name = {element ...}
traversal of an array
If it's in Python, minutes, a for, no conditions.
In Java:
Maximum index of array: array.length-1
// A for loop array in Java int New int [5]; for (int i=0;i<=arr.length-1;i++) { System.out.println (arr[i]);}
Overview and definition of two-dimensional arrays
Defined:
- An array that is essentially an array as an array of elements, that is, an array of arrays
The format of the two-dimensional array:
- data type [] Array name = new data type [M][n]
- Data type array name [] = new data type [M][n]
- data type [] array name [] = new data type [M][n]
declaration of the array:
Int[][] arr = {{1,2},{3,4}}
Add:
- The value of the base data type is passed without changing the original value, because the stack is played after the call and the local variable disappears
- The value of the reference data type is passed, changing the original value, because even if the method is on the stack, but the array inside the heap is still there, you can continue to access it through the address
Does the array in 1.Java arrive at the time of the pass value or address?
To be Continued ...
Java Learning Path (iii): Arrays in Java