Cainiao notes-multi-dimensional array and multi-dimensional array of notes

Source: Internet
Author: User

Cainiao notes-multi-dimensional array and multi-dimensional array of notes

1. Two-dimensional array

Equivalent to a table, Syntax: int [,] arr = new int [2, 3];

The access element is similar to accessing a cell, subscript [I, j]: [row, column]

GetLength (): This method obtains the dimension array of the array. The subscript starts from 0.

Traverse output:

1 int [,] arr = new int [6, 6]; 2 3 // GetLength (): Get the length of a dimension of the array 4 for (int I = 0; I <arr. getLength (0); I ++) 5 {6 for (int j = 0; j <arr. getLength (1); j ++) 7 {8 arr [I, j] = 6; 9 Console. write (arr [I, j]); 10} 11 Console. writeLine (); 12}View Code

 

 

2. Staggered Array

It is essentially a one-dimensional array, but the elements in the array are also a one-dimensional array with no length specified and not instantiated.

Note:

Creation Syntax:

Int [] [] arr = new int [3] [];

Assignment:

Arr [0] = new int [] {3, 6 };

 

Traverse the elements in the array in the staggered array:

  

1 for (int I = 0; I <arr2.Length; I ++) 2 {3 int [] arr3 = arr2 [I]; 4 if (arr3! = Null) // judge that the array stored in the array cannot be null 5 {6 for (int j = 0; j <arr3.Length; j ++) 7 {8 Console. writeLine (arr3 [j]); 9} 10} 11}View Code

 

 

 

Summary:

Declare an array. If the data type is the reference data type, the element in the array is null by default if no value is assigned to the array.

If this element is null, no error will be reported if the Console. WriteLine () is similar to the operation output. If you want to traverse and change it, an error will be reported:

Object reference is not set to the object instance

 

 

        

 

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.