First look at JAVA arrays

  • 2020-04-01 03:26:42
  • OfStack

1. One-dimensional array

1.1 definition of a one-dimensional array


type arrayName[];
type[] arrayName;

Where the type (type) can be an arbitrary data type in Java, containing a simple type combination type, and the arrayName arrayName is a valid identifier, [] indicating that the variable is an array type variable.

Another form of c + + developers may think it's very strange, just for development languages such as JAVA or c #, another form may be more intuitive, because of the just is a variable defined here, system did not in fact cases, only need to specify the type of the variable can, do not have to be specified in the [] array size. (is the first form just a way to keep up with old habits, given the influence of C?)

Such as:


int intArray[];
 Declares an integer array in which each element is an integer. with C , C++ Different, Java Memory is not allocated for array elements in the definition of an array, therefore [] There is no need to specify the number of elements in an array, that is, the length of the array, and you cannot question any element of an array as defined above. We have to allocate memory space for it, and we use the operator new , such as the following: 
arrayName=new type[arraySize];
 In the middle, arraySize Specifies the length of the array. Such as: 
intArray=new int[3];

Allocates the memory space occupied by three int integers to an integer array.

In general, the two parts can be combined in the following format:


type arrayName=new type[arraySize]; 
 Such as: 
int intArray=new int[3];
1.2 references to one-dimensional array elements

After you define an array and allocate memory space for it with the operator new, you can refer to each element in the array. The reference method of array elements is:
 
ArrayName [index]
Where: index is the index of an array, which can be an integer constant or an expression. For example, a[3], b[I] (I is an integer), c[6*I], etc. The index starts at 0, all the way to the length of the array minus 1. For the in-tarray number in the sample above, it has three elements, which are:
IntArray [0], intArray[1], intArray[2]. Note: there is no intArray[3].
 
Also, unlike in C and C++, Java overlooks array elements for security. At the same time, each array has a property length that specifies its length, for example, intarray.length specifies the length of an array.
 


public class ArrayTest{
  public static void main(String args[]){
    int i;
    int a[]=new int[5];
    for(i=0;i The < 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 ArrayTest
a[4]=4
a[3]=3
a[2]=2
a[1]=1
a[0]=0

The 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 as shown in the sample above. It can also be initialized at the same time as the array is defined.
Such as:
Int a[]={1,2,3,4,5};
The elements of the array are separated by commas (,), and the system allocates space to the array itself.
 
Unlike in C, where Java does not require an array to be static, in fact, the variable is similar to the pointer in C, so it is still valid to use it as a return value to other functions. In C, returning local variables to the calling function to continue using is a very easy mistake for anyone who has just started learning.
 
2. Multidimensional arrays

Like C and C++, multidimensional arrays in Java are considered arrays of arrays. For example, a two-dimensional array is a special one-dimensional array, each element of which is a one-dimensional array. In the following, we mainly take two-dimensional Numbers as an example to illustrate that the case of higher dimensions is similar.

2.1 definition of two-dimensional array

Two-dimensional array is defined as:

Type arrayName [] [];
Such as:
Int intArray [] [];

As with one-dimensional arrays, there is no memory allocated to the array elements, so use the operator new to allocate memory so that you can question each element.
For high-dimensional arrays, memory space can be allocated in the following ways:

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 respectively, such as:


int a[][]=new int[2][];
a[0]=new int[3];
a[1]=new int[3];

Finish 1 same function. This is different from C and C++, where the length of each dimension must be specified at once.

2.2 references to 2d array elements

For each element in the two-dimensional array, the reference method is: arrayName[index1][index2], in which index1 and index2 are subscripts, can be integer constants or expressions, such as a[2][3], etc., the same, each subscript starts from 0.

2.3 initialization of two-dimensional array

There are two ways:
1. Assign values to each element directly.
2. Initialize the array at the same time you define it.
 
Int a[][]={{2,3}, {1,5}, {3,4}};
Defines a 3 by 2 array and assigns values to each element.


Related articles: