1. One-dimensional array
1.1 Definition of one-dimensional array
type arrayName[];type[] arrayName;
The type (type) can be any data type in Java, including simple type combination types. The array name arrayName is a legal identifier, and [] indicates that the variable is an array type variable.
The other form may be very strange to C++ developers, but for development languages such as JAVA or C#, the other form may be more intuitive, because what is defined here is just a variable, and the system does not instantiate it. , you only need to specify the type of the variable, and there is no need to specify the array size in []. (Is the first form just for compatibility with past habits? After all, the influence of C language is too great?)
for example:
int intArray[]; declares an integer array, and each element in the array is integer data. Unlike C and C++, Java does not allocate memory for array elements in the definition of an array. Therefore, [] does not need to indicate the number of elements in the array, that is, the length of the array, and you cannot ask anything about an array defined as above. elemental. We must allocate memory space for it. At this time, we need to use the operator new. Its format is as follows: arrayName=new type[arraySize]; among them, arraySize specifies the length of the array. For example: intArray=new int[3];
Allocate the memory space occupied by 3 int type integers for an integer array.
Usually, these two parts can be combined, with the following format:
type arrayName=new type[arraySize]; For example: int intArray=new int[3];
1.2 Reference to one-dimensional array elements
After defining an array and allocating memory space for it using operator new, you can reference each element in the array. Array elements are referenced as:
arrayName[index]
Among them: index is the array subscript, which can be an integer constant or an expression. Such as a[3], b[i] (i is an integer), c[6*I], etc. The subscripts start from 0 and go up to the length of the array minus 1. For the in-tArray number in the above example, it has 3 elements, namely:
intArray[0], intArray[1], intArray[2]. Note: There is no intArray[3].
In addition, unlike C and C++, Java requires out-of-bounds checks on array elements to ensure safety. At the same time, each array has an attribute length that specifies its length, for example: intArray.length specifies 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]); }}
The execution results are as follows:
C:/>java ArrayTesta[4]=4a[3]=3a[2]=2a[1]=1a[0]=0
This program assigns a value to each element in the array and outputs it in reverse order.
1.3 Initialization of one-dimensional array
Array elements can be assigned values according to the above example. It can also be initialized at the same time as the array is defined.
for example:
int a[]={1, 2, 3, 4, 5};
Separate each element of the array with a comma (,), and the system will actively allocate a certain amount of space for the array.
Different from C, Java does not require the array to be static at this time. In fact, the variables here are similar to pointers in C, so it is still valid to use them as return values for other functions. In C, local variables Returning to the calling function to continue using it is a very easy mistake for people who are just learning.
2. Multidimensional array
Like C and C++, multidimensional arrays in Java are regarded as arrays of arrays. For example, a two-dimensional array is a special one-dimensional array, and each element is a one-dimensional array. Below we mainly use two-dimensional numbers as an example to illustrate. The situation in high dimensions is similar.
2.1 Definition of two-dimensional array
A two-dimensional array is defined as:
type arrayName[][];
for example:
int intArray[][];
Like the one-dimensional array, no memory space is allocated for the array elements at this time. At the same time, operator new must be used to allocate memory before each element can be accessed.
For high-dimensional arrays, there are several methods to allocate memory space:
1. Directly allocate space for each dimension, such as:
int a[][]=new int[2][3];
2. Starting from the highest dimension, allocate space for each dimension, such as:
int a[][]=new int[2][];a[0]=new int[3];a[1]=new int[3];
Complete the same function as in 1. This is different from C and C++, in which the length of each dimension must be specified once.
2.2 Reference to two-dimensional array elements
For each element in the two-dimensional array, the reference method is: arrayName[index1][index2] where index1 and index2 are subscripts, which can be integer constants or expressions, such as a[2][3], etc., the same, each One-dimensional subscripts all start from 0.
2.3 Initialization of two-dimensional array
There are two ways:
1. Directly assign a value to each element.
2. Initialize the array at the same time as it is defined.
For example: int a[][]={{2, 3}, {1, 5}, {3, 4}};
Define a 3×2 array and assign a value to each element.