zoukankan      html  css  js  c++  java
  • c#数组

    1 数组是引用类型的变量,元素保存在托管堆中元素的引用保存在栈中。交错数组和矩形数组的使用格式如下:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 
     6 namespace ArrayTest
     7 {
     8     class Program
     9     {
    10         static void Main(string[] args)
    11         {
    12             string[][] array=new string[2][];
    13             array[0] = new string[3] { "hello","world","!"};
    14             array[1] = new string[1] { "lzhq" };
    15             PrintArray(array);
    16             string[,] array1 = new string[2, 3];
    17             for (int i = 0; i < array1.GetLength(0); i++)
    18             {
    19                 for (int j = 0; j < array1.GetLength(1); j++)
    20                 {
    21                     array1[i, j] = string.Format("{0}{1}",i,j);
    22                 }
    23             }
    24             PrintArray(array1);
    25 
    26         }
    27         public static void PrintArray(string[][] array)
    28         {
    29             for (int i = 0; i < array.GetLength(0); i++)
    30             {
    31                 for (int j = 0; j < array[i].GetLength(0); j++)
    32                 {
    33                     Console.Write("array[{0}][{1}]={2}  ",i,j,array[i][j]);
    34                 }
    35                 Console.WriteLine();
    36             }
    37         }
    38         public static void PrintArray(string[,] array)
    39         {
    40             for (int i = 0; i < array.GetLength(0); i++)
    41             {
    42                 for (int j = 0; j < array.GetLength(1); j++)
    43                 {
    44                     Console.Write("array[{0},{1}]={2}",i,j,array[i,j]);
    45                 }
    46                 Console.WriteLine();
    47             }
    48         }
    49     }
    50 }

    GetLength(int dimension)可以获取数组指定维数中的元素数
    Length属性:可以获取数组所有维数中元素的总数

    Rank属性:可以获取数组的维数

    注意:数组声明中不允许出现任何关于维数的定义,长度只能出现在赋值运算符的右侧。只有在数组实例化的时候定义数组各维度的长度,此时数组大小的定义也是必须的必须提供。

    交错数组是多个一维数组的组合,多个一维数组之间依靠指针相关联。矩形数组是完全的多维数组。

    矩形三维数组的使用格式:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 
     6 namespace ArrayTest
     7 {
     8     class Program
     9     {
    10         static void Main(string[] args)
    11         {
    12             string[][] array=new string[2][];
    13             array[0] = new string[3] { "hello","world","!"};
    14             array[1] = new string[1] { "lzhq" };
    15             PrintArray(array);
    16             string[,] array1 = new string[2, 3];
    17             for (int i = 0; i < array1.GetLength(0); i++)
    18             {
    19                 for (int j = 0; j < array1.GetLength(1); j++)
    20                 {
    21                     array1[i, j] = string.Format("{0}{1}",i,j);
    22                 }
    23             }
    24             PrintArray(array1);
    25             string[, ,] array2 = new string[2, 3, 4];
    26             for (int i = 0; i < array2.GetLength(0); i++)
    27             {
    28                 for (int j = 0; j < array2.GetLength(1); j++)
    29                 {
    30                     for (int k = 0; k < array2.GetLength(2); k++)
    31                     {
    32                         array2[i, j, k] = string.Format("{0}{1}{2}",i,j,k);
    33                     }
    34                 }
    35             }
    36             PrintArray(array2);
    37 
    38 
    39         }
    40         public static void PrintArray(string[][] array)
    41         {
    42             for (int i = 0; i < array.GetLength(0); i++)
    43             {
    44                 for (int j = 0; j < array[i].GetLength(0); j++)
    45                 {
    46                     Console.Write("array[{0}][{1}]={2}  ",i,j,array[i][j]);
    47                 }
    48                 Console.WriteLine();
    49             }
    50         }
    51         public static void PrintArray(string[,] array)
    52         {
    53             for (int i = 0; i < array.GetLength(0); i++)
    54             {
    55                 for (int j = 0; j < array.GetLength(1); j++)
    56                 {
    57                     Console.Write("array[{0},{1}]={2}",i,j,array[i,j]);
    58                 }
    59                 Console.WriteLine();
    60             }
    61         }
    62         public static void PrintArray(string[, ,] array)
    63         {
    64             for (int i = 0; i < array.GetLength(0); i++)
    65             {
    66                 for (int j = 0; j < array.GetLength(1); j++)
    67                 {
    68                     for (int k = 0; k < array.GetLength(2); k++)
    69                     {
    70                         Console.Write("array[{0},{1},{2}]={3}  ",i,j,k,array[i,j,k]);
    71                     }
    72                 }
    73             }
    74         }
    75     }
    76 }

    所有数组类型的变量不管是一维数组,矩形数组还是交错数组,所有的数组都可以使用其索引访问其中的元素,都可以用索引定位到数组中的元素。

  • 相关阅读:
    vue指令(3)v-html
    vue指令(2)v-text
    vue基础(2)模板语法
    Struts2中在Action里面向前端页面传值的方法总结
    MySQL之字符串函数
    搜索关键字描红
    点击超链接执行js代码实现确认操作
    Java之线程同步的三种方法
    Java之线程的控制
    Java之线程的生命周期
  • 原文地址:https://www.cnblogs.com/moonfans/p/2805373.html
Copyright © 2011-2022 走看看