The Java language declares an array in two forms. Take a one-dimensional array as an example:
- Array type Array name []; This way is the writing habit of C language
- Array type [] Array name; this way is the Java writing habit
How to declare an array:
- Array type[] Array name = {em1,em2,em3,…,emN}; //initialize when declaring the array, a total of N elements, for example:
int[] array = {3,5,4,8,12,5};//一共六个元素
- Array type[] Array name = new Array type[N] //When declaring an array with the new keyword, specify the length of the array, for example:
String[] str = new String[6];
the length of the array is 6, that is, the array has six elements - Array type[] Array name = new Array type[] {em1,em2,em3,…,emN}; Initialize the array when declaring the array with the new keyword, for example: The
int[] array = new int[] {2,4,5,6,8,9};
array has five elements in total.
Once the array is declared, the length of the array is already determined. Every array has a length property, which cannot be changed. Array elements can be changed.