• C# string[,]与string[][]的区别


    对于这两者的区别:

    1.入门:string[,]可读可写,而string[][]与string[]相同,不可对第二位进行写操作

      

    static void Main(string[] args)
            {
    
                //声明变量
                string[] str1 = new string[10];
                string[,] str2 = new string[10, 10];
                string[][] str3=new string[10][];
                //string[][] str3 = new string[10][10];  //编译报错:无效的秩说明符: 应为“,”或“]”
    
                //赋值
                for (int i = 0; i < 10; i++)
                {
                    for (int j = 0; j < 10; j++)
                    {
                        str1[i] = i.ToString("00") + j.ToString("00");
                        //str1[i][j] = i.ToString("00") + j.ToString("00");   //编译不过:无法对属性或索引器“string.this[int]”赋值 -- 它是只读的
                        str2[i, j] = i.ToString() + j.ToString();
                    }
                }
    
                //输出str[][]
                System.Console.WriteLine("this is str[]:");
                for (int i = 0; i < 10; i++)
                {
                    System.Console.Write(str1[i]+" ");
                }
                System.Console.WriteLine();
                System.Console.WriteLine("and:");
                for (int i = 0; i < 10; i++)
                {
                    for (int j = 0; j < 4; j++)
                    {
                        System.Console.Write(str1[i][j] + " ");
                    }
                    System.Console.WriteLine();
                }
    
                //输出str[,]
                System.Console.WriteLine();
                System.Console.WriteLine("this is str[,]:");
                for (int i = 0; i < 10; i++)
                {
                    for (int j = 0; j < 10; j++)
                    {
                        System.Console.Write(str2[i,j]+" ");
                    }
                    System.Console.WriteLine();
                }
    
                System.Console.ReadKey();
            }

    2.进阶:当然是在内存中占的内存不同;

      稍后善后

  • 相关阅读:
    这一年来
    网络流复习笔记
    Codeforces Round #431
    Codeforces Round #398 (Div. 2)
    Codeforces Round #418 (Div. 2)
    【Codeforces 98E】 Help Shrek and Donkey 游戏策略神题
    【bzoj1878】[SDOI2009]HH的项链
    Round 403 div. 2
    Codeforces Round #417 (Div. 2)
    Codeforces Round #416 (Div. 2)
  • 原文地址:https://www.cnblogs.com/not-NULL/p/4740685.html
Copyright © 2020-2023  润新知