Java how to define arrays and initialization

Source: Internet
Author: User
1. Definition of array:

Arrays can be divided into one-dimensional arrays, two-dimensional arrays, and multidimensional arrays. The one-dimensional array is defined in the following two ways:

Type[] Arr_name;

Type arr_name[];

Two-dimensional arrays are defined in the following ways:

Type[] [] arr_name;

Type arr_name[] [];

A multidimensional array can specify the corresponding number of dimensions based on the dimension []

Int[] arr1;

String[] ARR2;

float arr3[];

String[] [] ARR4;

2. Initialization of the array:

The initialization of an array has the following two ways

Static initialization

Static initialization refers to specifying the contents of an array element while defining, with the sample code as follows:

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

String[] arr2 = {"Tom", "Rose", "Sunny"};

String[] Arr3 = {{"Tom", "American"},{"Jack", "England"},{"John", "the" "", "};

Dynamic initialization

Dynamic initialization is defined by first opening the specified size of storage space through the new keyword, and then specifying the contents in the array

int[] arr1 = new int[2];

Arr1[0] = 10;

ARR2[1] = 20;

arr3[2] = 30;

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.