1. Arrays are a reference data type in the Java language, which is an ordered set of data of the same type. Applies to multiple data with the same type of centralized management and similar purpose
an array of Μ Java requires that all array elements have the same data type. (Can be a basic data type, but also a reference data type)
Μ Once the array is initialized, the storage space that the array occupies in memory is fixed, so the length of the array cannot be changed.
Μ accesses an array element by its index number (or subscript).
The type of the Μ array element is unique, that is, only one data type data can be stored in an array, but not many.
2. The dimensions of an array can be understood as the number of levels of data combinations in an array, and an array of data combinations of one level is called a one-dimensional array. Two one-dimensional arrays are combined and the resulting array is called a two-dimensional array.
¯ the declaration of a one-dimensional array
format: type name variable name []; or type name [] variable name, whose position can be placed after the element type, before the data name, or after the data name, the effect is the same
For example: int[] arr; Double salary[];
string[] args; Student s[];
Μ declares an array type variable, the Run-time system allocates reference space only for reference variables, does not create objects, and does not allocate space for array elements, so you cannot use any of the array elements.
Μ The length of an array is not allowed when declaring an array type variable:
Int[3] A; X
3. Creation and use of array objects
in the Java language, you typically use the keyword new to create an array object with the syntax format: new type name [n]
for example, int [] arr;
Arr=new int[3];
4. Note: When accessing an array element, the legal subscript range is 0~n-1, and if the line is crossed, the program compiles without an error, but the program runs with the exception of the bounds.
5. Example 1: Declaration, creation, and use of one-dimensional arrays of basic data types.
int[] arr=new int[5];
for (int i=0;i<5;i++)
System.out.print (arr[i]+ "T"); Once an array is created, the element is initialized to the default value of 0
Example 2: declaration, creation, and use of a one-dimensional array of reference types.
point[] p;
p=new point[5];
for (int i=0;i<5;i++) {
p[i]=new Point (i,i+1);
P[i].display ();
reference type diagram:
6. Length property of an array object
An array object has a special attribute length (the length of a read-only method that is automatically provided by the system), which is of type int and is accessible directly, and is useful when traversing an array element.
For example, for (int i;i<arr.length;i++) ...
Note: The length of an array in the Java language is not part of its data type, and a variable of type int[] can point to an array object of type int[of length 3, or to an array object of int[of length 5, for example, Int[]a; a= New Int[3]; A=NEWINT[5];
7. Static initialization of arrays
If you can determine the values of its elements when you create an array object, you can use a simplified writing method to create and initialize the array objects.
For example: (1) int a[]={12,9,36};
(2) int a[]=new int (3);
a[0]=12;
a[1]=9;
a[2]=36;
For example, the static initialization of a reference type array is:
point[] p={new Point (3,5),
new Point ( -4,9),
new Point (2,-9),
new Point ( -5,-2)};//6 Line code is a statement
Note: (1) This simplified way of creating and initializing an array object is called static initialization of an array, (2) This method of creating an array object and assigning each array element separately is called dynamic initialization.
The so-called static initialization is only a simplified form of writing conventions, since the agreement can not be messed up, such as the following statement is illegal:
Int a[];
a={12,9,36}
Static initialization can only be output on one line. The so-called conventions here are mandatory rules that rule makers consider useful and effective, and as a guideline for the coding behavior of Java programmers, the Java language specification is actually made up of a series of conventions (grammatical rules).