A. Basic concepts of Arrays
- Arrays can be thought of as multiple combinations of the same type of data, which are managed uniformly.
- An array variable is a reference type, and an array can be considered an object, and each element in the array is equivalent to the member variable of that object.
- The elements of an array can be any data type, including the base type and the reference type.
- Arrays in C and C + + can be allocated on the stack, whereas arrays in Java can be allocated only on the heap, because arrays in Java are reference types.
Two One-dimensional arrays
There are 2 ways to declare one-dimensional arrays:
- Format one: array element type array name []; That is, type var[];
- Format two: array element type [] array name; i.e. type[] var;
- Format two declares an array in the same way that you declare a one-dimensional array on C #.
Example: int a1[]; Int[] A2;
Double b[];
Person[] P1; String s1[];
Note: You cannot specify a length (the number of elements in an array) when declaring an array in the Java language
such as: int a[5]; It is illegal to declare a one-dimensional array.
three. Model of the array
- One-dimensional array: one-dimensional array is a row, a row of small cells.
- Two-dimensional array: a two-dimensional array is a row and a column composed of a plane into a small lattice, there are rows and columns.
- Three-dimensional array: a three-dimensional array is a cube.
- Humans are most aware of the three-dimensional space.
Four. Creation of array Objects
Use the keyword new in Java to create an array object.
Format: Array name = New array element type [number of array elements]
For example:
Five. element is an array of reference data types
Note: Each element in an array that is a reference data type needs to be instantiated.
For example:
Class date{
int year; int moth; int day;
Date (int y; int m, int d) {
Year=y;
Month=m;
Day=d;
}
}
Six. Initialization of the array
- 1. Dynamic initialization
The array definition is performed separately from the operation of allocating space and assigning values to arrays.
For example:
1 Public classtest{2 Public Static voidMain (String args[]) {3 intA[];//defines an array, which declares an array of type int a[]4A=New int[3];//allocates memory space for array elements. 5a[0]=3; a[1]=9; a[2]=8;//assigns a value to an array element. 6 Date days[];7days=NewDate[3];8days[0]=NewDate (1, 4, 2004);9days[1]=NewDate (2, 4, 2004);Tendays[2]=NewDate (3, 4, 2004); One } A } - - classdate{ the intYear , month, day; -Date (intYintMintd) { -Year =y; -month =m; +Day =D; - } + } A
allocates space and assigns values to an array element while it is being defined.
For example:
Puclicclasstest{ Public Static voidMain (String args[]) {intA[] = {3, 9, 8};//allocates space and assigns values to the array while defining the array. Date days[] = { NewDate (1, 4, 2004), NewDate (2, 4, 2004), NewDate (3, 4, 2004) }; }}classdate{intYear , month, day; Date (intYintMintd) { Year=y; Month=m; Day=D; }}
Seven. Default initialization of array elements
- An array is a reference type whose elements are equivalent to the member variables of the class, so after allocating memory space to the array, each element is implicitly initialized by the rules of the member variable.
1 Public classtest{2 Public Static voidMain (String args[]) {3 intA[] =New int[5];4Date[] Days=NewDate[3];5System.out.println (a[3]);6System.out.println (days[2]);7 }8 }9 classdate{Ten intYear , month, day; OneDate (intYintMintd) { AYear =y; -month =m; -Day =D; the } -}
System.out.println (A[3]); The printed result is: 0.
System.out.println (days[2]); The result of printing is: null (NULL)
Eight. References to array elements
After the definition is allocated with the operator new, each element in the array can be referenced, and the array element is referenced by:Arrayname[index], and the index is an array-element subscript, which can be an integer constant or an integral expression. such as:a[3], b[i], c[6*i].
Array element subscript starts at 0, and the valid subscript value range for an array of length n is 0 ~ n-1.
Each array has a property length that indicates its size, for example: The A.length value is the length of the arrayA (number of elements).
Nine. Two-dimensional arrays
Ten
Understanding the array model of each dimension in Java
An array of data structures