与java类似的 int[][] a定义数组,但是却有着完全不同的意思。
交错数组是数组的数组。
交错数组是一维数组
交错数组的每一个成员都是一个一维数组(每个一维数组包含的成员可以不同)

您可以声明一个带有
int** 值的交错数组 scores,如下所示:
int [][] scores;
声明一个数组不会在内存中创建数组。创建上面的数组:

  1. int[][] scores = new int[5][];
  2. for (int i = 0; i < scores.Length; i++)
  3. {
  4. scores[i] = new int[4];
  5. }

您可以初始化一个交错数组,如下所示:

  1. int[][] scores = new int[2][]{new int[]{92,93,94},new int[]{85,66,87,88}};//每一个成员都是一个单独的一维数组

其中,scores 是一个由两个整型数组组成的数组 — scores[0] 是一个带有 3 个整数的数组,scores[1] 是一个带有 4 个整数的数组。

实例

下面的实例演示了如何使用交错数组:

实例

  1. using System;
  2. namespace ArrayApplication
  3. {
  4. class MyArray
  5. {
  6. static void Main(string[] args)
  7. {
  8. /* 一个由 5 个整型数组组成的交错数组 */
  9. int[][] a = new int[][]{new int[]{0,0},new int[]{1,2},
  10. new int[]{2,4},new int[]{ 3, 6 }, new int[]{ 4, 8 } };
  11. int i, j;
  12. /* 输出数组中每个元素的值 */
  13. for (i = 0; i < 5; i++)
  14. {
  15. for (j = 0; j < 2; j++)
  16. {
  17. Console.WriteLine("a[{0}][{1}] = {2}", i, j, a[i][j]);
  18. }
  19. }
  20. Console.ReadKey();
  21. }
  22. }
  23. }

交错数组内部一定是数组,因为无法将int型转化为int型。