交错数组(C# 编程指南)

交错数组是元素为数组的数组。 交错数组元素的维度和大小可以不同。 交错数组有时称为“数组的数组”。以下示例说明如何声明、初始化和访问交错数组。

下面声明一个由三个元素组成的一维数组,其中每个元素都是一个一维整数数组:

 
    
 
    
int[][] jaggedArray = new int[3][];
int[][] jaggedArray = new int[3][];
 
    
 
    
 
    
 
    
 
    
 
    
 
    
 
    

必须初始化 jaggedArray 的元素后才可以使用它。 可以如下例所示初始化该元素:

 
    
 
    
jaggedArray[0] = new int[5];
jaggedArray[1] = new int[4];
jaggedArray[2] = new int[2];
jaggedArray[0] = new int[5];
jaggedArray[1] = new int[4];
jaggedArray[2] = new int[2];
 
    
 
    
 
    
 
    
 
    
 
    
 
    
 
    

每个元素都是一个一维整数数组。 第一个元素是由 5 个整数组成的数组,第二个是由 4 个整数组成的数组,而第三个是由 2 个整数组成的数组。

也可以使用初始值设定项用值填充数组元素,在这种情况下不需要数组大小。 例如:

 
    
 
    
jaggedArray[0] = new int[] { 1, 3, 5, 7, 9 };
jaggedArray[1] = new int[] { 0, 2, 4, 6 };
jaggedArray[2] = new int[] { 11, 22 };
jaggedArray[0] = new int[] { 1, 3, 5, 7, 9 };
jaggedArray[1] = new int[] { 0, 2, 4, 6 };
jaggedArray[2] = new int[] { 11, 22 };
 
    
 
    
 
    
 
    
 
    
 
    
 
    
 
    

还可以在声明数组时将其初始化,如:

 
    
 
    
int[][] jaggedArray2 = new int[][] 
{
    new int[] {1,3,5,7,9},
    new int[] {0,2,4,6},
    new int[] {11,22}
};
int[][] jaggedArray2 = new int[][] 
{
    new int[] {1,3,5,7,9},
    new int[] {0,2,4,6},
    new int[] {11,22}
};
 
    
 
    
 
    
 
    
 
    
 
    
 
    
 
    

可以使用下面的速记格式。 请注意:不能从元素初始化中省略 new 运算符,因为不存在元素的默认初始化:

 
    
 
    
int[][] jaggedArray3 = 
{
    new int[] {1,3,5,7,9},
    new int[] {0,2,4,6},
    new int[] {11,22}
};
int[][] jaggedArray3 = 
{
    new int[] {1,3,5,7,9},
    new int[] {0,2,4,6},
    new int[] {11,22}
};
 
    
 
    
 
    
 
    
 
    
 
    
 
    
 
    

交错数组是数组的数组,因此其元素是引用类型并初始化为 null

可以如下例所示访问个别数组元素:

 
    
 
    
// Assign 77 to the second element ([1]) of the first array ([0]):
jaggedArray3[0][1] = 77;
// Assign 88 to the second element ([1]) of the third array ([2]):
jaggedArray3[2][1] = 88;
// Assign 77 to the second element ([1]) of the first array ([0]):
jaggedArray3[0][1] = 77;

// Assign 88 to the second element ([1]) of the third array ([2]):
jaggedArray3[2][1] = 88;
 
    
 
    
 
    
 
    
 
    
 
    
 
    
 
    

可以混合使用交错数组和多维数组。 下面声明和初始化一个一维交错数组,该数组包含大小不同的三个二维数组元素。 有关二维数组的详细信息,请参阅 多维数组(C# 编程指南)

 
    
 
    
int[][,] jaggedArray4 = new int[3][,] 
{
    new int[,] { {1,3}, {5,7} },
    new int[,] { {0,2}, {4,6}, {8,10} },
    new int[,] { {11,22}, {99,88}, {0,9} } 
};
int[][,] jaggedArray4 = new int[3][,] 
{
    new int[,] { {1,3}, {5,7} },
    new int[,] { {0,2}, {4,6}, {8,10} },
    new int[,] { {11,22}, {99,88}, {0,9} } 
};
 
    
 
    
 
    
 
    
 
    
 
    
 
    
 
    

可以如本例所示访问个别元素,该示例显示第一个数组的元素 [1,0] 的值(值为 5):

 
    
 
    
System.Console.Write("{0}", jaggedArray4[0][1, 0]);
System.Console.Write("{0}", jaggedArray4[0][1, 0]);
 
    
 
    
 
    
 
    
 
    
 
    
 
    
 
    

方法 Length 返回包含在交错数组中的数组的数目。 例如,假定您已声明了前一个数组,则此行:

 
    
 
    
System.Console.WriteLine(jaggedArray4.Length);
System.Console.WriteLine(jaggedArray4.Length);
 
    
 
    
 
    
 
    
 
    
 
    
 
    
 
    

返回值 3。

本例生成一个数组,该数组的元素为数组自身。 每一个数组元素都有不同的大小。

 
     
 
     
class ArrayTest
{
    static void Main()
    {
        // Declare the array of two elements:
        int[][] arr = new int[2][];

        // Initialize the elements:
        arr[0] = new int[5] { 1, 3, 5, 7, 9 };
        arr[1] = new int[4] { 2, 4, 6, 8 };

        // Display the array elements:
        for (int i = 0; i < arr.Length; i++)
        {
            System.Console.Write("Element({0}): ", i);

            for (int j = 0; j < arr[i].Length; j++)
            {
                System.Console.Write("{0}{1}", arr[i][j], j == (arr[i].Length - 1) ? "" : " ");
            }
            System.Console.WriteLine();            
        }
        // Keep the console window open in debug mode.
        System.Console.WriteLine("Press any key to exit.");
        System.Console.ReadKey();
    }
}/* Output:
    Element(0): 1 3 5 7 9
    Element(1): 2 4 6 8
*/
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值