Definition and usage of Java arrays

Source: Internet
Author: User

An array is a collection of ordered data, and each element in the array has the same array name and subscript to uniquely determine the elements in the array.

1. One-dimensional arrays

1.1 Definition of one-dimensional array

Type arrayname[];

Type[] Arrayname;

The type can be a random data type in Java, contains a simple type combination type, the array name arrayname to a valid identifier, [] indicates that the variable is an array type variable.

Another form for C + + developers may be considered very strange, but for Java or C # This development language, another form may be more intuitive, because the definition here is only a variable, the system is not actually instantiated, just need to indicate the type of the variable can be, do not need to specify the array size []. (The first form is not just to be compatible with the habits of the past, 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 integral type of data. Unlike C, C + +, Java does not allocate memory for array elements in the definition of arrays, so [] does not indicate the number of elements in the array, that is, the length of the array, and for an array as defined above, no matter what element it is. We have to allocate memory space for it, then we use the operator new, which has the following format:

Arrayname=new Type[arraysize];

The arraysize indicates the length of the array. Such as:

Intarray=new Int[3];

Allocates a 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];

Like what:

int intarray=new int[3];

1.2 References to one-dimensional array elements

Once you have defined an array and allocated memory space for it with the operator new, you can refer to each element in the array. The array elements are referenced in the following way:

Arrayname[index]

In: 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 above example, it has 3 elements, respectively:

INTARRAY[0],INTARRAY[1],INTARRAY[2]. Note: no intarray[3].

In addition, unlike C, C + +, Java array elements are cross-checked to ensure security. At the same time, for each array there is a property length that indicates its lengths, such as: 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]); }}

The results of the execution 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 then outputs it in reverse order.

1.3 Initialization of one-dimensional arrays

An array of elements can be assigned according to the sample above. It is also possible to initialize at the same time as the array is defined.

Like what:

int a[]={1,2,3,4,5};

Separate the elements of the array with commas (,), and the system itself allocates a certain amount of space for the arrays.

Unlike C, when Java does not require the array to be static (static), in fact, the variable is similar to the pointer in C, so it is used as a return value to other functions, is still valid, in C to return the local variable to the calling function to continue to use is just beginning to learn the person very easy to make mistakes.

2. Multidimensional arrays

Like 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 of its elements is a one-dimensional array. Here we mainly take the two-dimensional number as an example to illustrate that the high-dimensional situation is similar.

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 is not allocated memory space, and the operator new is used 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. At the beginning of the highest dimension, allocate space for each dimension, for example:

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, C + +, and must indicate the length of each dimension once in C and C + +.

2.2 References to two-dimensional array elements

For each element in a two-dimensional array, the reference is: Arrayname[index1][index2], index1, index2 as subscript, can be integer constant or expression, such as a[2][3], and so on, each dimension of the subscript from 0 start.

2.3 Initialization of two-dimensional arrays

There are two ways of doing this:

1. Assign a value directly to each element.

2. Initialize at the same time as the array is defined.

such as: int a[][]={{2,3},{1,5},{3,4}};

An array of 3x2 is defined, and each element is assigned a value.

Definition and usage of Java arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.