. Net BASICS (4) -- Array

Source: Internet
Author: User
Array

Task C # VB
Create a 5-dimensional array with the default value as the initial value Int [] ages = new int [5];
Console. WriteLine (ages. Length); // => 5
Console. WriteLine (ages. Rank); // => 1
Console. WriteLine (ages. GetLowerBound (0); // => 0
Console. WriteLine (ages. GetUpperBound (0); // => 4
Dim ages (4) As Integer
Console. WriteLine (ages. Length) '=> 5
Console. WriteLine (ages. Rank) '=> 1
Console. WriteLine (ages. GetLowerBound (0) '=> 0
Console. WriteLine (ages. GetUpperBound (0) '=> 4
Create a 5-dimensional array and specify the Initial Value Int [] ages = new int [5] {1, 3, 5, 7, 9}; // OK
Int [] ages1 = new int [] {1, 3, 5, 7, 9}; // OK
Int [] ages2 = new int [6] {1, 3, 5, 7, 9}; // compilation Error
Console. WriteLine (ages. Length); // => 5
Console. WriteLine (ages. Rank); // => 1
Console. WriteLine (ages. GetLowerBound (0); // => 0
Console. WriteLine (ages. GetUpperBound (0); // => 4
Dim ages () As Integer = New Integer (4) {1, 3, 5, 7, 9} 'OK
Dim ages1 () As Integer = New Integer () {1, 3, 5, 7, 9} 'OK
Dim ages2 () As Integer = New Integer (5) {1, 3, 5, 7, 9} 'compilation Error
Console. WriteLine (ages. Length) '=> 5
Console. WriteLine (ages. Rank) '=> 1
Console. WriteLine (ages. GetLowerBound (0) '=> 0
Console. WriteLine (ages. GetUpperBound (0) '=> 4
Create a two-row, three-column two-dimensional array with the default value as the initial value Int [,] t = new int [2, 3];
Console. WriteLine (t. Length); // => 6
Console. WriteLine (t. Rank); // => 2
Console. WriteLine (t. GetLength (0); // => 2
Console. WriteLine (t. GetLowerBound (0); // => 0
Console. WriteLine (t. GetUpperBound (0); // => 1
Console. WriteLine (t. GetLowerBound (1); // => 0
Console. WriteLine (t. GetUpperBound (1); // => 2
Console. WriteLine (t. GetLength (1); // => 3
Dim t (1, 2) As Integer
Console. WriteLine (t. Length) '=> 6
Console. WriteLine (t. Rank) '=> 2
Console. WriteLine (t. GetLength (0) '=> 2
Console. WriteLine (t. GetLowerBound (0) '=> 0
Console. WriteLine (t. GetUpperBound (0) '=> 1
Console. WriteLine (t. GetLowerBound (1) '=> 0
Console. WriteLine (t. GetUpperBound (1) '=> 2
Console. WriteLine (t. GetLength (1) '=> 3
Create a two-row, three-column two-dimensional array and specify the initial value. Int [,] t = new int [2, 3] {1, 2, 3 },
{4, 5, 6 }};
Int [,] t1 = new int [,] {1, 2, 3 },
{4, 5, 6 }};
Console. WriteLine (t. Length); // => 6
Console. WriteLine (t. Rank); // => 2
Console. WriteLine (t. GetLength (0); // => 2
Console. WriteLine (t. GetLowerBound (0); // => 0
Console. WriteLine (t. GetUpperBound (0); // => 1
Console. WriteLine (t. GetLowerBound (1); // => 0
Console. WriteLine (t. GetUpperBound (1); // => 2
Console. WriteLine (t. GetLength (1); // => 3
Dim t (,) As Integer = New Integer (1, 2) {1, 2, 3 },_
{4, 5, 6 }}
Dim t1 (,) As Integer = New Integer (,) {1, 2, 3 },_
{4, 5, 6 }}
Console. WriteLine (t. Length) '=> 6
Console. WriteLine (t. Rank) '=> 2
Console. WriteLine (t. GetLength (0) '=> 2
Console. WriteLine (t. GetLowerBound (0) '=> 0
Console. WriteLine (t. GetUpperBound (0) '=> 1
Console. WriteLine (t. GetLowerBound (1) '=> 0
Console. WriteLine (t. GetUpperBound (1) '=> 2
Console. WriteLine (t. GetLength (1) '=> 3
Create a one-dimensional array with a length of 5 subscript of 1 and use the default value as the initial value. Array t = Array. CreateInstance (typeof (string), new int [] {5}, new int [] {1 });
Console. WriteLine (t. Length); // => 5
Console. WriteLine (t. Rank); // => 1
Console. WriteLine (t. GetLength (0); // => 5
Console. WriteLine (t. GetLowerBound (0); // => 1
Console. WriteLine (t. GetUpperBound (0); // => 5
Dim t As Array = Array. CreateInstance (GetType (Integer), New Integer () {5}, New Integer () {1 })
Console. WriteLine (t. Length) '=> 5
Console. WriteLine (t. Rank) '=> 1
Console. WriteLine (t. GetLength (0) '=> 5
Console. WriteLine (t. GetLowerBound (0) '=> 1
Console. WriteLine (t. GetUpperBound (0) '=> 5
Create a two-dimensional array with two rows and three columns subscript of 1 and use the default value as the initial value. Int [,] t = (int [,]) Array. CreateInstance (typeof (int), new int [] {2, 3}, new int [] {1, 1 });
Console. WriteLine (t. Length); // => 6
Console. WriteLine (t. Rank); // => 2
Console. WriteLine (t. GetLength (0); // => 2
Console. WriteLine (t. GetLowerBound (0); // => 1
Console. WriteLine (t. GetUpperBound (0); // => 2
Console. WriteLine (t. GetLowerBound (1); // => 1
Console. WriteLine (t. GetUpperBound (1); // => 3
Console. WriteLine (t. GetLength (1); // => 3
Dim t (,) As Integer = Array. CreateInstance (GetType (Integer), New Integer () {2, 3}, New Integer () {1, 1 })
Console. WriteLine (t. Length) '=> 6
Console. WriteLine (t. Rank) '=> 2
Console. WriteLine (t. GetLength (0) '=> 2
Console. WriteLine (t. GetLowerBound (0) '=> 1
Console. WriteLine (t. GetUpperBound (0) '=> 2
Console. WriteLine (t. GetLowerBound (1) '=> 1
Console. WriteLine (t. GetUpperBound (1) '=> 3
Console. WriteLine (t. GetLength (1) '=> 3
Change the array Length Int [] t = new int [2];
Array. Resize (ref t, 3 );
Console. WriteLine (t. Length); // => 3
Console. WriteLine (t. Rank); // => 1
Console. WriteLine (t. GetLength (0); // => 3
Console. WriteLine (t. GetLowerBound (0); // => 0
Console. WriteLine (t. GetUpperBound (0); // => 2
Dim t (2) As Integer
Array. Resize (t, 3)
Console. WriteLine (t. Length) '=> 3
Console. WriteLine (t. Rank) '=> 1
Console. WriteLine (t. GetLength (0) '=> 3
Console. WriteLine (t. GetLowerBound (0) '=> 0
Console. WriteLine (t. GetUpperBound (0) '=> 2
Forced type conversion of referenced Array String [] s = new string [3];
Object [] OS = s;
S = (string []) OS;
Dim s (2) As String
Dim OS () As Object = s
S = OS
Forced type conversion of value type array (VS2005) Int [] a = new int [2];
Decimal [] da1 = a; // compilation Error
Decimal [] da2 = (int []) a; // compilation Error
Decimal [] da = Array. ConvertAll (,
New Converter (
Delegate (int old)
{
Return old;
}); // OK
Dim a (2) As Integer
Dim da () As Decimal = Array. ConvertAll (,_
New Converter (Of Integer, Decimal) (AddressOf ConvertInt ))
...
Function ConvertInt (ByVal old As Integer) As Decimal
Return old
End Function
Forced type conversion of Value Type arrays (VS2008) Int [] a = new int [2];
Decimal [] da = Array. ConvertAll (,
New Converter (Old => old ));
Dim a (2) As Integer
Dim da () As Decimal = Array. ConvertAll (Of Integer, Decimal) (,_
New Converter (Of Integer, Decimal) (Function (old ))
Create an interleaved array (an array) Int [] [] a = new int [2] [];
Console. WriteLine (a [0] = null); // => True
Dim a (2) () As Integer
Console. WriteLine (a (0) Is Nothing) '=> True
Create an interleaved array and initialize it (Example 1) Int [] [] a = new int [2] [] {new int [2], new int [3]};
Console. WriteLine (a [0]. Length); // => 2
Console. WriteLine (a [1]. Length); // => 3
Dim m (1) As Integer
Dim n (2) As Integer
Dim a () As Integer = New Integer (1) () {m, n}
Console. WriteLine (a (0). Length) '=> 2
Console. WriteLine (a (1). Length) '=> 3
Create an interleaved array and initialize it (Example 2) Int [] [] a = new int [2] [] {new int [2] {1, 2 },
New int [3] {11, 22, 33 }};
Console. WriteLine (a [0] [0]); // => 1
Console. WriteLine (a [1] [2]); // => 33
Dim m () As Integer = New Integer (1) {1, 2}
Dim n () As Integer = New Integer (2) {11, 22, 33}
Dim a () As Integer = New Integer (1) () {m, n}
Console. WriteLine (a (0) (0) '=> 1
Console. WriteLine (a (1) (2) '=> 33

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.