数组

任务 C# VB
创建一个长度为5的一维数组,使用默认值作为初值 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
创建一个长度为5的一维数组,指定初始值 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 }; // 编译错误
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} ' 编译错误
Console.WriteLine(ages.Length)  '=> 5
Console.WriteLine(ages.Rank)  '=> 1
Console.WriteLine(ages.GetLowerBound(0))  ' => 0
Console.WriteLine(ages.GetUpperBound(0))  '=> 4
创建一个2行3列二维数组,使用默认值作为初值 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
创建一个2行3列二维数组,指定初始值 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
创建一个长度为5下标为1的一维数组,使用默认值作为初值 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
创建一个2行3列下标为1的二维数组,使用默认值作为初值 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
改变数组长度 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
引用类型数组的强制类型转换 string[] s = new string[3];
object[] os = s;
s = (string[])os;
Dim s(2) As String
Dim os() As Object = s
s = os
值类型数组的强制类型转换(VS2005) int[] a = new int[2];
decimal[] da1 = a; // 编译错误
decimal[] da2 = (int[])a; // 编译错误
decimal[] da = Array.ConvertAll(a,
    new Converter(
        delegate(int old)
        {
            return old;
        })); // OK
Dim a(2) As Integer
Dim da() As Decimal = Array.ConvertAll(a, _
    New Converter(Of Integer, Decimal)(AddressOf ConvertInt))

Function ConvertInt(ByVal old As Integer) As Decimal
    Return old
End Function
值类型数组的强制类型转换(VS2008) int[] a = new int[2];
decimal[] da = Array.ConvertAll(a,
    new Converter(old => old));
Dim a(2) As Integer
Dim da() As Decimal = Array.ConvertAll(Of Integer, Decimal)(a, _
    New Converter(Of Integer, Decimal)(Function(old) old))
创建一个交错数组(即数组的数组) int[][] a = new int[2][];
Console.WriteLine(a[0] == null); //=> True
Dim a(2)() As Integer
Console.WriteLine(a(0) Is Nothing) '=> True
创建一个交错数组,并初始化(例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
创建一个交错数组,并初始化(例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


posted on 2008-03-15 15:09  1-2-3  阅读(1915)  评论(4编辑  收藏  举报