Definition of a one-dimensional array and a two-dimensional array in Java

Source: Internet
Author: User
Tags array definition

In Java, arrays are regarded as an object.

When defining an array, there are two defining methods: int [] A and int A []; the second is the way C/C ++ defines the array, we recommend that you use the first definition method for Java.

General principle: Any object must be initialized before being called!


1. Definition of one-dimensional array


// Define a one-dimensional array containing three elements

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



Int [] A = new int [3];

A [0] = 1;

A [1] = 2;

A [2] = 3;

Method 1: Do not write as follows:

Int [] A = new int [3];

A = {1, 2, 3 };

The reason is that new int [3] is used to create an object. The object has been initialized and assigned an initial value of 0. You can use the following code to verify the object:

Int [] d = new int [3];

For (INT I = 0; I <D. length; I ++ ){

System. Out. println (d [I]);

}

Input result: 0 0 0

If a = {1, 2, 3}; is used to assign values, we 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. initialize the new object directly.


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

Method 3: Do not write in this way: int [] C = new int [3] {1, 2, 3 };

That is, This method cannot specify dimensions. Cause: If array Initialization is specified, dimension expressions cannot be defined.



Note: If you use new to define an array, you must specify its dimension. In this case, the definition is incorrect: int [] d = new int [];

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

2. Two-dimensional array Definition


Similar to a one-dimensional array


// Define a two-dimensional array with three rows and five columns

// Method 1, first 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}, {, 2}, {, 3 }};

 

// Method 3. initialize the new object directly.


Int [] [] A = new int [] []

{, 1}, {, 2}, {, 3, 3 }};





To define a two-dimensional array, you must specify the number of rows, and the number of columns can be specified.

The definition is correct: int [] [] d = new int [3] [];

This definition is incorrect: int [] [] d = new int [] [4]; int [] [] d = new int [] [];

You can also define irregular Arrays:

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 a one-dimensional array int [] B = {, 3}; B. length is 3,

For a two-dimensional array, first study its configuration details: int [] [] arr = new int [2] [3];

In the above program snippet
2
Line
3
A two-dimensional array object of a column, because the data type is
Int
So the default element of the array element is
0
.

Actually
Arr [0]
,
Arr [1]
Is two one-dimensional array objects, each of which has a length
3
, And
Arr
Type is
Int [] []
, The content value is
Arr [0]
And
Arr [1]
. Its Relationship 1
.


Figure
1
Configuration relationship of two-dimensional arrays

Slave chart
1
,
Arr
Reference
Int [] []
Type object, including
Arr [0]
And
Arr [1]
, And
Arr [0]
And
Arr [1]
Reference a one-dimensional array object.

Therefore
Arr. Length
And
Arr [0]. Length
The length is different. Arr. Length indicates the number of rows in the array, and ARR [I]. Length indicates the number of elements contained in the specified row. In this example, arr. Length = 2, arr [0]. Length = 3.




4. Initial Value of the array


Quantity
Data
Class
Type

Chu
 
Start
 
Value

Quantity
Data
Class
Type

Chu
 
Start
 
Value

Byte

0

Float

0.0f

Short

0

Double

0.0d

Int

0

Char

/U0000

Long

0l

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.