Sunmmervocation_learning--java Creation of arrays

Source: Internet
Author: User

One-dimensional array declaration method:type var[] or type[] Var, such as int a[], int[] A;

declaring an array in Java cannot specify its length, such as int a[5] is illegal.

creation of one-dimensional array objects: use New in Java to create an array object. The format is: var = new Type[length];

such as: int[] s; s = new int[5];. That is, a member variable S is declared in the stack space, and an array object of length 5 is created in the heap space.

Use S to point to this object. And the object is automatically initialized according to the type in the heap space, such as the initial deposit of 5 0 in S.

If the array element type is not eight basic types, it is a reference type.

Then each element in an array of reference types needs to be instantiated.

For example, suppose a date class is defined. and creates an array of the date class, constructed by date (Int,int,int): date[] D = new date[3];

Then each date[i] needs to be instantiated. D[0] = new Date (2018,7,8);
D[1] = new Date (2018,7,9);

d[2] = new Date (2017,7,10);

The array length can be var.length. Where length is a property, not a method. So no parentheses are added.

Initialization of two-dimensional arrays:should go from left to right. such as int a[][] = new int[3][]; but int a[][] = new int[][3] is illegal.

Static initialization of two-dimensional arrays, cannot specify length:

int a[][]={{},{2,3},{3,4}} is legal. but int a[3][2]={{},{2,3},{3,4}} is illegal.

Dynamic initialization of two-dimensional arrays:

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];

Sunmmervocation_learning--java Creation of arrays

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.