Preface
The recent use of Java in the topic of ACM, often forget how to implement static initialization of arrays, so here is the common sense of Java array use.
Java Array Common sense
An array is an object in Java, and an array instance needs to be created with the new operator.
Common sense Knowledge:
- The size of the array can be obtained by Array.Length.
- The array index starts with 0 and the negative index is invalid in Java. Also, unlike C-language arrays, arrays in Java are inferred out of bounds. An invalid index throws arrayindexoutofboundexception.
- An array of contiguous memory spaces stored in the Java heap.
- An array is a fixed-length data structure. Once the length of the array is declared. The subsequent operation can no longer change the length of the array.
- An array is a very fast data structure. Assuming you have been able to determine how much data you have, you should use arrays instead of data structures such as ArrayList.
initialization of Java arrays
The initialization of Java arrays is divided into dynamic initialization and static initialization.
As an example of an array of type int, the static initialization and dynamic initialization are described separately.
Static Initialization
intnewint[] {123456};
As can be seen from the above code, static initialization is the initial value of each element of the specified array, and the system determines the length of the array.
Dynamic Initialization
intnewint[6];
As can be seen from the code above, dynamic initialization simply specifies the length of the array, and the system assigns the initial values to the arrays.
Different types. The initial value of the system assignment is also different.
| type |
Initial value |
| Integer type (byte,short,int,long) |
0 |
| Floating-point type (float,double) |
0.0 |
| Character type (char) |
' \u0000 ' (representing spaces) |
| Boolean Type (Boolean) |
False |
| Reference type (class, interface, array) |
Null |
Java Array Memo