6th array Creating arrays and initializing arrays

Source: Internet
Author: User

1, create a one-dimensional, two-dimensional array, there are two methods, respectively:

1) Declare first and then use the new operator for memory allocation

One-dimensional: int arr[]; Declares an array of type int, each element in the array is of type int int[] arr;

Two-dimensional: int arr[][]; Int[][] arr;

   after declaring an array , you cannot immediately access any of its elements , because declaring an array simply gives the array name and the data type of the element, and if you want to actually use the array, allocate memory space for it, When allocating memory space for an array, you must indicate the length of the arrays .

One-dimensional: arr=new int[5];

Two-dimensional: arr=new int[2][4];

2) allocating memory for arrays at the same time as declared

One-dimensional: int month[]=new int[12];

Two-dimensional: int month[][]=new int[2][2];

2. Initialize one-dimensional, two-dimensional arrays

One-dimensional: int arr[]=new int[]{1,2,3}; int arr[]={1,2,3};

Two-dimensional: int arr[][]=new int[][]{{1,2},{3,4}};  int arr[][]={{1,2},{3,4}}; Explanation: A[0][0]=1, a[0][1]=2, a[1][0]=3, a[1][1]=4

6th array Creating arrays and initializing arrays

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.