one. Basic concepts of Arrays
· Arrays can be viewed as multiple combinations of the same type of data, which are managed uniformly.
· An array variable is a reference type, and an array can also be considered an object, and each element in the array corresponds to a member variable of that object.
· The elements of an array can be any data type, including basic types and reference types.
· Arrays in C and C + + can be allocated on top of the stack, while arrays in Java can only be allocated on the heap, because arrays in Java are reference types.
two. One-dimensional arrays
There are 2 ways to declare a one-dimensional array:
· Format one: array element type array name []; That is, type var[];
· Format two: array element type [] array name; namely type[] var;
· The format two declares an array in the same way that a one-dimensional array is declared on C #.
For example: int a1[]; Int[] A2;
Double b[];
Person[] P1; String s1[];
Note: You cannot specify its 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. The model of an array
· One-dimensional array: A one-dimensional array is a row, a row of small cells.
· Two-dimensional array: A two-dimensional array is a row and a column of a plane into the small lattice, there are rows of columns.
· Three-dimensional array: three-dimensional array is a cube.
· A multidimensional array essentially stores an array as an object in an array.
Four. The creation of an array object
Memory allocation in Java when an array object is created using the keyword new .
The format is: 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 references the 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 arrays
Dynamic initialization: When an array is created, the system defaults to the elements in the array, and you specify the length
Static initialization: When an array is created, the programmer explicitly assigns an initial value to the elements in the array, and you do not specify the length of the arrays
Implicit initialization of an array: Because an array is a reference type and its elements are equivalent to a member variable of a class, the array allocates space, and each element is implicitly initialized according to the rules of the member variable.
One-dimensional arrays
1) int[] A; Declaration, no initialization
int a[];
A[0]=1; Error because the array is not initialized and cannot be assigned a value
a[1]=2;
2) int[] a=new int[5]; Initialize to default value, int is 0 (dynamic)
Int[] A;
A=new Int[5]; Right, both ways.
3) int[] a={1,2,3,4,5}; Initialize to a given value (static)
Int[] A;
a={1,2,3,4,5}; Error, array constants can only be used in initialization operations, as
4) int[] A=new int[]{1,2,3,4,5}; Same (3) (static)
Int[] a=newint[5]{1,2,3,4,5}; Error, you cannot define a dimension expression if an array initialization operation is provided
Two-dimensional array
1) int[][] A; Declaration, no initialization
2) int[][] a=new int[2][3]; Initialize to default value, int is 0
3) int[][] a={{1,2},{2,3},{3,4}; Initialized to the given value
Int[][] a={{1,2},{2,3},{3,4,5}}; There is no fault, the array space is not continuously allocated, so the size of each dimension is not required
4) int[][] A=new int[2][];
A[0]=new Int[3]; A[0] is actually an array
A[1]=new Int[4]; The size of each dimension can be different;
5) int[][] A=new int[][]{{1,2},{2,3},{3,4,5}; Same (3)
Int[] A=new int[5]{{1,2},{2,3},{3,4,5}}; Error, you cannot define a dimension expression if an array initialization operation is provided
Int[][] a=newint[2][];
a[0]={1,2,3,4,5}; Error, array constants can only be used in initialization operations
6) int[][] A=new int[2][];
A[0][1]=1; Error, second dimension not initialized, cannot assign value, Java.lang.NullPointerException exception
Summary: 1, the array before use (assignment, access) must be initialized, can be initialized with the new default, can also be initialized with array constants;
2, two-dimensional is an array of arrays, inside the size of the array does not require the same
· 1. Dynamic initialization
The array definition is separate from the operations that allocate space and assignment for an array element.
For example:
1 public class test{
2 public static void Main (String args[]) {
3 int a[]; Defines an array, which declares an array of type int a[]
4 A=newint[3]; Allocates memory space to array elements.
5 a[0]=3; a[1]=9; a[2]=8; Assigns a value to an array element.
6 Date days[];
7 Days=new date[3];
8 Days[0]=new Date (1, 4, 2004);
9 Days[1]=new Date (2, 4, 2004);
Days[2]=newdate (3, 4, 2004);
11}
12}
13
Class date{
Intyear, month, day;
Date (int y,int m,int d) {
Year = y;
month = m;
Day = D;
20}
21}
22
· 2. Static initialization
allocates space and assigns values to array elements while defining arrays.
For example:
Puclic classtest{
public static Voidmain (String args[]) {
int a[] = {3, 9, 8}; Allocates space and assigns values to the array while defining the array.
Date days[] = {
New Date (1, 4, 2004),
New Date (2, 4, 2004),
New Date (3, 4, 2004)
};
}
}
Class date{
int year, month, day;
Date (int y, int m, INTD) {
Year = y;
month = m;
Day = D;
}
}
Seven. Default initialization of array elements
· An array is a reference type whose elements correspond to the member variables of the class, so that after allocating memory space to the array, each element is implicitly initialized according to the rules of the member variable.
1 public class test{
2 public static void Main (String args[]) {
3 int a[] = new INT[5];
4 date[] Days=new date[3];
5 System.out.println (a[3]);
6 System.out.println (days[2]);
7}
8}
9 Class date{
Intyear, month, day;
One Date (int y,int m,int d) {
Year = y;
month = m;
Day = D;
15}
16}
Output results:
System.out.println (A[3]); The results printed are: 0.
System.out.println (days[2]); The result is: null (empty)
When an array is initialized, the stored data type is the base data type, and its default value is the default value when the base data type is initialized. For example, the default value of integer type is 0;
The Boolean default value is False, and so on. The default value for the reference data type is null. Arrays also belong to reference data types. So the array in B holds an array and the one-dimensional array is a reference data type, and the default initialization value is NULL.
int [][]b = new int[3][];
b = {null, NULL, NULL};
Eight. Reference to an array element
Defines and allocates memory space with operator new to refer to each element in the array, the array element is referred to as:Arrayname[index], the index is subscript for an array element, and can be an integer constant or an integral type expression. such as:a[3], B[i],c[6*i].
The index of an array element starts at 0, and the valid subscript value range of an array of length n is 0 ~ n-1.
Each array has one attribute length that indicates its length, for example, the length of thea.length value for array A (number of elements).
Nine. Two-dimensional array