The initialization of a Java two-dimensional array and the use of its length

Source: Internet
Author: User

Initialization of two-dimensional arrays

1. Static initialization

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

In the Java language, because the two-dimensional array is considered an array of arrays, the array space is not continuously allocated, so the size of each dimension of the two-dimensional array is not required .

2. Dynamic initialization

1) Allocate space directly for each dimension, in the following format:

Arrayname = new Type[arraylength1][arraylength2];

int a[] [] = new INT[2][3];

2) Allocate space for each dimension starting from the highest dimension:

Arrayname = new type[arraylength1][];

Arrayname[0] = new TYPE[ARRAYLENGTH20];

ARRAYNAME[1] = new TYPE[ARRAYLENGTH21];

Arrayname[arraylength1-1] = new TYPE[ARRAYLENGTH2N];

3) Dynamic initialization of two-dimensional arrays of simple data types is as follows:

int a[] [] = new int[2][];

A[0] = new INT[3];

A[1] = new INT[5];

For arrays of two-dimensional composite data types, you must first allocate the reference space for the highest dimension , and then allocate the space in the lower dimension sequentially. Also, you must allocate space for each array element individually . For example:

String s[[] = new string[2][];

s[0]= new string[2];//Allocating reference space for the highest dimension

s[1]= New string[2]; Allocating reference space for the highest dimension

s[0][0]= new String (good);//Allocate space separately for each array element

s[0][1]= new String (Luck);//Allocate space separately for each array element

s[1][0]= new String (to);//Allocate space separately for each array element

s[1][1]= new String (you);//Allocate space separately for each array element

When using a two-dimensional array object, note the length represented by
The array name is followed by a length (such as Arr.length), which refers to a few rows (row);
Specifies the index with length (such as Arr[0].length), which is the number of elements owned by the row, that is, the column.

The initialization of a Java two-dimensional array and the use of its length

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.