C#齿形数组

简介: 齿形数组时数组的一类,它的行也是数组---行数组中的元素个数可以不同。如string[][] names; 接着names=new string[4][],创建了四个数组,它们的元素时字符串数组 names[0]=new string[3] names[0][0]="Jason"; name...

齿形数组时数组的一类,它的行也是数组---行数组中的元素个数可以不同。如string[][] names;

接着names=new string[4][],创建了四个数组,它们的元素时字符串数组

names[0]=new string[3]

names[0][0]="Jason";

names[0][1]="Marcus";

names[0][2]="Price";

names[1]=new string[2];

names[0][0]="Steve";

names[0][1]="Smith";

例:

/*
  Example10_9.cs illustrates the use of a jagged array
*/

using System;

class Example10_9
{

  public static void Main()
  {

    // declare a jagged array of four rows,
    // with each row consisting of a string array
    string[][] names = new string[4][];

    // the first row is an array of three strings
    names[0] = new string[3];
    names[0][0] = "Jason";
    names[0][1] = "Marcus";
    names[0][2] = "Price";

    // the second row is an array of two strings
    names[1] = new string[2];
    names[1][0] = "Steve";
    names[1][1] = "Smith";

    // the third row is an array of four strings
    names[2] = new string[] {"Cynthia", "Ann", "Jane", "Williams"};
    names[3] = new string[] {"Gail", "Jones"};

    // display the Rank and Length properties for the names array
    Console.WriteLine("names.Rank = " + names.Rank);
    Console.WriteLine("names.Length = " + names.Length);

    // display the Rank and Length properties for the arrays
    // in each row of the names array
    for (int row = 0; row < names.Length; row++)
    {
      Console.WriteLine("names[" + row + "].Rank = " + names[row].Rank);
      Console.WriteLine("names[" + row + "].Length = " + names[row].Length);
    }

    // display the array elements for each row in the names array
    for (int row = 0; row < names.Length; row++)
    {
      for (int element = 0; element < names[row].Length; element++)
      {
        Console.WriteLine("names[" + row + "][" + element + "] = " +
          names[row][element]);
      }
    }

  }

}
相关文章
|
7月前
|
存储 C语言
|
8月前
|
Java
数组的练习
数组的练习
|
8月前
|
存储 算法 计算机视觉
数组
数组
65 0
|
7月前
数组(2)
数组(2)
36 1
|
8月前
|
存储 C++ 索引
c++数组
c++数组
73 2
|
8月前
|
存储 搜索推荐 程序员
C++ 数组
C++ 数组
55 0
|
存储 C语言 索引
C 数组
C 数组。
44 0
|
算法
三 数组
三 数组
63 0
|
C# 索引
C#视频-数组
C#视频-数组
59 0
|
存储 C# 索引
C#视频—数组
C#视频—数组