Java two-dimensional array initialization and its length, java two-dimensional array Initialization

Source: Internet
Author: User

Java two-dimensional array initialization and its length, java two-dimensional array Initialization

Two-dimensional array Initialization

1. Static Initialization

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

In Java, because two-dimensional arrays are regarded as arrays, the array space is not continuously allocated.The size of each dimension of a two-dimensional array is not required to be the same..

 

2. Dynamic Initialization

1) directly allocate space for each dimension. The format is as follows:

ArrayName = new type [arrayLength1] [arrayLength1];

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

 

2) from the highest dimension, allocate space for each dimension:

ArrayName = new type [arrayLength1] [];

ArrayName [0] = new type [arrayLength20];

ArrayName [1] = new type [arrayLength21];

 

ArrayName [arrayLength1-1] = new type [arrayLength2n];

 

3) the dynamic initialization of a two-dimensional simple data array is as follows:

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

A [0] = new int [3];

A [1] = new int [5];

An array of two-dimensional Composite data types must beFirst, allocate reference space for the maximum dimensionAnd then allocate space for the low dimension. And,Space must be allocated separately for each array element. For example:

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

S [0] = new String [2]; // allocate reference space for the maximum dimension

S [1] = new String [2]; // allocate reference space for the maximum dimension

S [0] [0] = new String (Good); // allocate space for each array element

S [0] [1] = new String (Luck); // allocate space for each array element

S [1] [0] = new String (to); // allocate space for each array element

S [1] [1] = new String (You); // allocate space for each array element

 

When using a two-dimensional array object, pay attention to the length represented by length,
Add length (such as arr. length) directly after the array name, which refers to rows );
Add length (for example, arr [0]. length) after the specified index, which refers to the elements owned by the row, that is, the number of columns.

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.