Java array definition and usage

Source: Internet
Author: User
Tags array definition

An array is a set of ordered data. Each element in an array has the same array name and subscript to uniquely identify the elements in the array.

 

1. One-dimensional array

 

1.1 Definition of a one-dimensional array

 

Type arrayname [];

Type [] arrayname;

 

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

 

The second form may be strange to C ++ developers. However, for Java or C # development languages, the second form may be more intuitive, the definition is only a variable, and the system does not instantiate it. You only need to specify the type of the variable, and you do not need to specify the array size in. (Is the first form just to be compatible with previous habits? After all, the impact of C language is too great ?)

 

For example:

Int intarray [];

Declares an integer array. Each element in the array is an integer. Unlike C and C ++, Java does not allocate memory for array elements in array definition. Therefore, [] does not specify the number of elements in the array, that is, the length of the array, in addition, an array defined above cannot access any of its elements. We must allocate memory space for it. The new operator is used in the following format:

Arrayname = new type [arraysize];

Arraysize indicates the length of the array. For example:

Intarray = new int [3];

Allocate the memory space occupied by three int Integers to an integer array.

 

Generally, these two parts can be combined in the following format:

Type arrayname = new type [arraysize];

For example:

Int intarray = new int [3];

 

1.2 References to one-dimensional array elements

Defines an array and allocates memory space for it with the new operator. Then, each element in the array can be referenced. The reference method of array elements is as follows:

 

Arrayname [Index]

Here, index is an array subscript, which can be an integer constant or expression. For example, a [3], B [I] (I is an integer), and C [6 * I. The subscript starts from 0 until the length of the array is reduced by 1. For the in-tarray number in the preceding example, it has three elements:

Intarray [0], intarray [1], intarray [2]. Note: There is no intarray [3].

 

In addition, unlike C and C ++, Java checks the array elements out of bounds to ensure security. At the same time, each array has a property length to specify its length. For example, intarray. Length indicates the length of the array intarray.

 

Public class arraytest {<br/> Public static void main (string ARGs []) {<br/> int I; <br/> int A [] = new int [5]; <br/> for (I = 0; I <5; I ++) <br/> A [I] = I; <br/> for (I =. length-1; I> = 0; I --) <br/> system. out. println ("A [" + I + "] =" + A [I]); <br/>}< br/> 

 

The running result is as follows:

C:/> JAVA arraytest

A [4] = 4
A [3] = 3
A [2] = 2
A [1] = 1
A [0] = 0

This program assigns values to each element in the array and then outputs the values in reverse order.

 

1.3 initialize a one-dimensional array

You can assign values to array elements according to the preceding example. You can also Initialize an array.

For example:

Int A [] = {1, 2, 3, 4, 5 };

Use commas (,) to separate the elements of an array. The system automatically allocates space for the array.

 

Different from C, Java does not require the array to be static. In fact, the variable here is similar to the pointer in C. Therefore, it is still valid to use it as the return value to other functions, returning local variables to the calling function in C is a very easy mistake for beginners.

 

 

2. Multi-dimensional array

Similar to C and C ++, multi-dimensional arrays in Java are considered as arrays. For example, a two-dimensional array is a special one-dimensional array, and each element is a one-dimensional array. Next we will take two-dimensional numbers as an example to illustrate that the high-dimensional situations are similar.

 

2.1 Definition of two-dimensional array

The two-dimensional array is defined as follows:

Type arrayname [] [];

 

For example:

Int intarray [] [];

 

Like a one-dimensional array, no memory space is allocated to the array elements. You must use the new operator to allocate memory before accessing each element.

For high-dimensional arrays, there are several ways to allocate memory space:

 

1. Directly allocate space for each dimension, such:

Int A [] [] = new int [2] [3];

 

2. From the maximum 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 functions in 1. This is different from C and C ++. In C and C ++, the length of each dimension must be specified at a time.

 

2.2 References to two-dimensional array elements

For each element in a two-dimensional array, the reference method is: arrayname [index1] [index2] Where index1 and index2 are subscript and can be an integer constant or expression, for example, a [2] [3]. Similarly, the bottom mark of each dimension starts from 0.

 

2.3 initialize a two-dimensional array

There are two methods:

1. assign values to each element directly.

2. initialize the array at the same time.

 

For example, int A [] [] ={{ 2, 3}, {1, 5}, {3, 4 }};

Defines a 3 × 2 array and assigns values to each element.

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.