Definition of one-dimensional array and two-dimensional array in JAVA __java

Source: Internet
Author: User

In Java, an array is considered an object

When you define an array, there are two definitions: int[] A and int a[]; the second is a C + + array-defined method, with the first definition of Java recommendations.

The general principle: any object must be initialized before being called.

definition of 11-D arrays

Defines a one-dimensional array that contains three elements

//Method 1, first the new object, and then initialize each element

Int[] A = new int[3];

A[0] = 1;

A[1] = 2;

A[2] = 3;

Method One should note that it cannot be written like this:

Int[] A = new int[3];

A = {1,2,3};

The reason is to create the object in this way with new int[3, the object has been initialized and assigned an initial value of 0, which can be validated with the following code:

Int[] D = new int[3];

for (int i=0;i<d.length;i++) {

System.out.println (D[i]);

}

Enter the result: 0 0 0

If you use a = {1,2,3}; To assign a value, you know that constants cannot be assigned and cannot be modified.


//Method 2, directly assign the initial value to create the object

Int[] B = {1,2,3};


//Method 3,new object to initialize directly

Int[] c = new int[]{1,2,3};

Method Three to note is not to write: int[] c = new int[3]{1,2,3};

This means that the dimension cannot be specified because the dimension expression cannot be defined if an array initialization operation is specified.


Note: If you define an array with new, you must specify its dimensions, so the definition is wrong: int[] D = new int[];

If you cannot determine the number of its elements, you can define this: int[] e = {};

definition of 22-D arrays

is basically similar to a one-dimensional array

Defines a two-dimensional array of 3 rows and 5 columns

//Method 1, first the new object, and then initialize each element

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

A[0][0]=1;

a[0][1]=2;

a[0][2]=3;

//Method 2, directly assign the initial value to create the object

Int[][] B = {1,1,1,1,1}, {2,2,2,2,2}, {3,3,3,3,3}};

//Method 3,new object to initialize directly

Int[][] A = new int[][] {{1,1,1,1,1}, {2,2,2,2,2}, {3,3,3,3,3}};


Defining a two-dimensional array must specify its number of rows, the number of columns can be specified, and may not be specified.

This definition is correct: int[][] D = new int[3][];

This definition is wrong: int[][] D = new int[][4]; Int[][] D = new int[][];

You can also define an irregular array:

arr = new int[2][];

Arr[0] = new INT[3];

ARR[1] = new INT[5];

3 Length of the array

Length is an attribute of an array (not a Method!), for one-dimensional arrays int[] b = {1,2,3}; The value of B.length is 3,

For two-dimensional arrays, first look at its configuration details: int[][] arr = new INT[2][3];

In this program fragment, a two-dimensional array object with 2 rows and 3 columns is configured, because the data type is int, so the preset element of the array element is 0.

In fact Arr[0], arr[1] are two one-dimensional array objects, each with a length of 3, and the ARR type is int[] [], and the content value is arr[0] and arr[1]. The relationship is shown in Figure 1.

Figure 12-dimensional array configuration relationship

As you can see in Figure 1, arr references int[] [] types of objects, which include arr[0] and arr[1], and arr[0] and arr[1 respectively refer to a one-dimensional array object.

Therefore, the length meaning of arr.length and arr[0].length is different, arr.length represents the number of rows in the array, and Arr[i].length represents the number of elements that the specified row contains. In this case, arr.length=2,arr[0].length=3.


4 The initial value of the array

0L

| td>

Data type

Start   start   value

Data type

Start   start   value

Byte

p>0

Float

0.0f

Short

0

Double

0.0d

int

0

Char

/u0000

Long

Boolean

False

Related Article

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.