Static and dynamic initialization of one-dimensional, two-dimensional arrays in Java

Source: Internet
Author: User

Today we're going to start talking about arrays in Java, including static initialization and dynamic initialization of one-dimensional arrays and two-dimensional arrays

Array Overview:

The array can be regarded as a combination of multiple identical types of data, and the unified management of these data;

An array variable is a reference data type, and an array can be considered an object, and each element in the array is equivalent to the member variable of that object;

The elements in the array can be any data type, including the base data type and the reference data type;

Declaration of one-dimensional array:

Way of declaring: for example; int a [] = new int [3];

A declaration in the Java language cannot specify its length [number of elements in an array];

unlawful declaration; int a [5];

Creation of Array objects:

public class Test {

public static void Main (String args[]) {

int [] s;

s = new int [5];

for (int i = 0; i < 5; i + +) {

S[i] = 2 * i + 1;

}

}

}

One-dimensional array initialization

Dynamic initialization:

public class Test {

public static void Main (String args []) {

int a [];

a = new int [3];

int a [] = {n/a};

Date days [];

days = new Date [3];

Days [0] = new Date (1,4,20040);

Days [1] = new Date (2,4,20040);

Days [2] = new Date (3,4,20040);

}

}

Class Date {

int year,month,day;

Date (int y,int m,int d) {

Year = y; month = m; Day = D;

}

}

Static initialization

public class Test {

public static void Mian (String args []) {

int a[] = new int [] {3,9,8};

Date days[] = {

New Date (1,4,2004),

New Date (2,4,2004),

New Date (3,4,2004)

};

}

}

Class Date {

int year,month,day;

Date (int y,int m,int d) {

Year = y; month = M;day = D;

}

}

Two-dimensional arrays

A two-dimensional array can be considered an array of elements, such as:

int a [] = {{1,2},{3,4,5,6},{7,8,9}};

Two-dimensional array initialization

Static initialization:

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

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

Dynamic initialization:

int a [] [] = new int [3] [5];

int b [] [] = new int [3] [];

B[0] = new int [2];

B[1] = new int [3];

B[2] = new int [5];

Static and dynamic initialization of one-dimensional, two-dimensional arrays in Java

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.