1. One-dimensional arrays
1.1 Definition of a one-dimensional array
Type arrayname[];
Type[] Arrayname;
The type can be a random data type in Java, contains a simple type combination type, an array name arrayname as a valid identifier, [] indicates that the variable is an array type variable.
Another form may be considered very strange to C + + developers, just for Java or C # This development language, another form may be more intuitive, because the definition is only a variable, the system is not a de facto case, just need to indicate the type of the variable can, and do not need to specify the size of the array. (The first form is not just for the compatibility of the past habits, after all, the impact of C language is too big?) )
Like what:
int intarray[];
An integer array is declared, and each element in the array is an integer data. Unlike C/C + +, Java does not allocate memory for array elements in the definition of arrays, so there is no need to point out the number of elements in an array, that is, the length of an array, and no matter what elements are not available for an array as defined above. We have to allocate the memory space for it, then we need to use operator new, in the format of the following:
arrayname=new type[arraysize];
, arraysize indicates the length of the array. such as:
intarray=new int[3];
Allocates the memory space occupied by 3 int integers for an integer array.
Typically, these two parts can be combined in a format such as the following:
Type arrayname=new type[arraysize];
For example:
int intarray=new int[3];
1.2 Reference to a one-dimensional array element
After defining an array and allocating the memory space to it with operator new, you can refer to each element in the array. The array elements are referenced in the following ways:
Arrayname[index]
In which: index is an array subscript, which can be an integer constant or an expression. such as A[3],b[i] (i is integral type), c[6*i] and so on. The subscript starts at 0 and continues to the length of the array minus 1. For the In-tarray number in the example above, it has 3 elements, respectively:
INTARRAY[0],INTARRAY[1],INTARRAY[2]. Note: no intarray[3].
In addition, unlike C, C + +, Java array elements are checked across borders to ensure security. At the same time, for each array, there is an attribute length indicating its lengths, for example: Intarray.length indicates the length of the array intarray.
public class arraytest{public
static void Main (String args[]) {
int i;
int a[]=new int[5];
for (i=0;i<5;i++)
a[i]=i;
for (i=a.length-1;i>=0;i--)
System.out.println ("a[" +i+ "]=" +a[i));
}
Execution results such as the following:
C:/>java arraytest
a[4]=4
a[3]=3
a[2]=2
a[1]=1
The program assigns values to each element in the array and then outputs it in reverse order.
1.3 Initialization of one-dimensional arrays
An array element can be assigned according to the sample above. can also be initialized at the same time that the array is defined.
Like what:
int a[]={1,2,3,4,5};
By separating the elements of the array with commas (,), the system itself actively allocates a space for the group.
Unlike C, Java does not require the array to be static (static). In fact, the variable here is similar to the pointer in C, so it is still valid to use it as a return value to other functions, and returning the local variable to the calling function in C continues to be a very easy mistake for people who are just starting to learn.
2. Multidimensional Arrays
As with C, C + +, multidimensional arrays in Java are treated as arrays of arrays. For example, a two-dimensional array is a special one-dimensional array, and each element is a one-dimensional array. The following is an example of a two-dimensional number, which is similar in the case of high-dimensional.
2.1 Definition of two-dimensional arrays
Two-dimensional arrays are defined in the following ways:
Type arrayname[][];
Like what:
int intarray[][];
As with a one-dimensional array, the array element does not allocate memory space, with the operator new to allocate memory, and then the ability to access each element.
For high-dimensional arrays, there are several ways to allocate memory space:
1. Allocate space directly for each dimension, such as:
int a[][]=new int[2][3];
2. From the highest dimension, each space is allocated for each dimension, such as:
int a[][]=new int[2][];
A[0]=new int[3];
A[1]=new Int[3];
Complete the same function in 1. This is different from C and C + +, and must indicate the length of each dimension in C and C + +.
2.2 Reference to two-dimensional array elements
For each element in a two-dimensional array, the reference mode is: Arrayname[index1][index2] in which index1, index2 as subscript, can be integral type constant or expression, such as a[2][3], the same, each dimension of the subscript are starting from 0.
2.3 Initialization of two-dimensional arrays
There are two ways of doing this:
1. Assign values directly to each element.
2. Initialize at the same time that the array is defined.
such as: int a[][]={{2,3},{1,5},{3,4}};
Defines an array of 3x2 and assigns values to each element.