最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#中多維數(shù)組[,]和交錯(cuò)數(shù)組[][]的區(qū)別

 更新時(shí)間:2022年01月10日 10:11:12   投稿:yangbin  
這篇文章介紹了C#中多維數(shù)組[,]和交錯(cuò)數(shù)組[][]的區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

多維數(shù)組的聲明

在聲明時(shí),必須指定數(shù)組的長(zhǎng)度,格式為 type [lenght ,lenght ,lengh, ... ]

int [,] test1 = new int [3,3];

或聲明時(shí)即賦值,由系統(tǒng)推斷長(zhǎng)度

int [,] test1 = {
            {1,2,3},
            {1,2,3},
            {1,2,3},
        };

交錯(cuò)數(shù)組的聲明

聲明時(shí),至少需要指定第一維的長(zhǎng)度,格式為 type [ ] [ ] [ ] ...

int [][] test1 = new int[5][];
int [][] test1 = new int[][];    //注意,此的聲明方式是錯(cuò)的

或者聲明時(shí)即賦值,由系統(tǒng)推斷長(zhǎng)度

int [][] test1 = {
    new int[] {1,2,3,4},
    new int[] {1,2,3},
    new int[] {1,2}
};

多維數(shù)組與交錯(cuò)數(shù)組 二者的相同、區(qū)別

兩者聲明時(shí),都必須指定長(zhǎng)度,多維數(shù)組必須指定每一維的長(zhǎng)度,而交錯(cuò)數(shù)組需要至少需要指定第一維的長(zhǎng)度。

多維數(shù)組聲明時(shí),符號(hào)是這樣的 [ , , , , ],逗號(hào)在 方括號(hào) [ ] 中,每一維長(zhǎng)度用逗號(hào)分隔。而交錯(cuò)數(shù)組每一維獨(dú)立在 [ ]中

當(dāng)你想指定數(shù)組長(zhǎng)度時(shí),只能在等號(hào)右側(cè)指定,int [,] test1 = new int [3,3] 是正確的 ;int [6,4] test1 = new int [6,4] 是錯(cuò)誤的;

下面以代碼形式說(shuō)明

大小不一致的多維數(shù)組會(huì)發(fā)生錯(cuò)誤

int [,] test1 = {
            {1,2,3,4},
            {1,2,3},
            {1,2}
        };         //這樣是錯(cuò)的,長(zhǎng)度必須一致
int [,] test1 = new int [4,5] {
            {1,2,3,4,5},
            {1,2,3},
            {1,2,3}
        };        //這樣也是錯(cuò)誤的,長(zhǎng)度必須一致,必須為每一個(gè)位置賦值

這一點(diǎn)C#與C語(yǔ)言有所區(qū)別,C語(yǔ)言可以不全賦值,沒(méi)有賦值的位置系統(tǒng)默認(rèn)為0。

下面的方法是正確的

int [,] test1 = {
            {1,2,3},
            {1,2,3},
            {1,2,3}
        };

初始化交錯(cuò)數(shù)組

上面已經(jīng)說(shuō)了聲明一個(gè)交錯(cuò)數(shù)組的方法

int [][] test1 = {
          new int[] {1,2,3,4},     //new int[4] {1,2,3,4}
          new int[] {1,2,3},      //new int[3] {1,2,3}
          new int[] {1,2}
      };

注意,在里面有new int[],這正是交錯(cuò)數(shù)組的特性。

交錯(cuò)數(shù)組是由數(shù)組構(gòu)成的數(shù)組,交錯(cuò)數(shù)組要求為內(nèi)部的每個(gè)數(shù)組都創(chuàng)建實(shí)例。

即交錯(cuò)數(shù)組的每一維都是一個(gè)實(shí)例,每一個(gè)實(shí)例為一個(gè)數(shù)組。

數(shù)組的長(zhǎng)度是固定的

無(wú)論多維數(shù)組還是交錯(cuò)數(shù)組,長(zhǎng)度都是固定的,不能隨意改變。

獲取數(shù)組的長(zhǎng)度

使用 對(duì)象.Length 獲取數(shù)組的長(zhǎng)度,需要注意的是,多維數(shù)組的長(zhǎng)度是每一維相乘,即元素總個(gè)數(shù)。

       int [,] test1 = {
           {1,2,3},
           {1,2,3},
           {1,2,3}
       };
       Console.WriteLine(test1.Length);
輸出為   9

而交錯(cuò)數(shù)組的長(zhǎng)度則是“內(nèi)部組成的數(shù)組的個(gè)數(shù)”,例如

int [][] test1 = {
     new int[] {1,2,3},
     new int[] {1,2,3},
     new int[] {1,2,3},
 };
 Console.WriteLine(test1.Length);
 輸出為 3

方法

多維數(shù)組、交錯(cuò)數(shù)組的方法無(wú)差別,都具有Sort()、Clear()等方法,這里不再贅述,關(guān)于數(shù)組的高級(jí)用法,請(qǐng)查閱

http://m.fzitv.net/Special/265.htm

下列為System.Array類的屬性

由于系統(tǒng)提供的方法比較多,有興趣請(qǐng)查閱

https://docs.microsoft.com/zh-cn/dotnet/api/system.array?view=netframework-4.7.2

使用數(shù)組初始化類型

在C#中有 lambda、匿名類等等,C# 5.0/6.0 后,給聲明類、聲明類型類型、賦值等有了很方便的操作方法。下面舉例測(cè)試。

例子1

使用數(shù)組對(duì)集合、集合泛型等初始化

聲明一個(gè) List 泛型集合

using System.Collections.Generic;        //頭部引入

    //main中的代碼
        static void Main(string[] args)
        {
            List<string> list = new List<string>();

            Console.ReadKey();
        }

那么,給集合 list 增加一個(gè)項(xiàng),用 Add() 方法

        static void Main(string[] args)
        {
            List<string> list = new List<string>();
            //增加
            list.Add("a");
            list.Add("b");
            list.Add("c");
            list.Add("d");
            list.Add("e");
            list.Add("f");
            list.Add("g");
            Console.ReadKey();
        }

利用 “數(shù)組” 來(lái)添加新項(xiàng)

List<string> list = new List<string>(){"a","b","c","d","e","f"}; 

List<string> list = new List<string>{"a","b","c","d","e","f"};

//以上兩種方法都可以,注意后面有沒(méi)有 ()

例子2

上面的例子利用數(shù)組直接在集合聲明時(shí)初始化,但是不能很好的聲明“騷操作”。

試試交錯(cuò)數(shù)組

1,在 program類 所在的命名空間中寫一個(gè)類

    public class Test
    {
        public int x;
        public int y;
        public void What()
        {
            Console.WriteLine(x + y);
        }
    }

2,在 Main 方法中

       static void Main(string[] args)
        {
            List<Test> list = new List<Test>()
            {
                new Test{x=1,y=6},
                new Test{x=8,y=6},
                new Test{x=4,y=8},
                new Test{x=5,y=7},
                new Test{x=3,y=3},
                new Test{x=6,y=6},
                new Test{x=9,y=666},
                new Test{x=7,y=6},
            };
            Console.ReadKey();
        }

完整代碼如下

    public class Test
    {
        public int x;
        public int y;
        public void What()
        {
            Console.WriteLine(x + y);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            List<Test> list = new List<Test>()
            {
                new Test{x=1,y=6},
                new Test{x=8,y=6},
                new Test{x=4,y=8},
                new Test{x=5,y=7},
                new Test{x=3,y=3},
                new Test{x=6,y=6},
                new Test{x=9,y=666},
                new Test{x=7,y=6},
            };
            Console.ReadKey();
        }
    }

由于類引用類型,它的內(nèi)存是引用地址,不像 int、char等類型,所以在對(duì)類(引用類型)使用數(shù)組、集合等形式時(shí),可以用 “交錯(cuò)數(shù)組” 來(lái)理解。

以上所述是小編給大家介紹的C#中多維數(shù)組[,]和交錯(cuò)數(shù)組[][]的區(qū)別,希望對(duì)大家有所幫助。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論

阳曲县| 英德市| 天全县| 西安市| 内乡县| 玉环县| 南城县| 万全县| 明星| 贵阳市| 盘锦市| 山西省| 拉萨市| 大理市| 兴和县| 武定县| 沾益县| 仙游县| 洪雅县| 萝北县| 揭西县| 灵丘县| 丹棱县| 大厂| 永修县| 息烽县| 米脂县| 靖州| 全南县| 巴东县| 资阳市| 宁南县| 禄丰县| 北票市| 溧水县| 新野县| 惠安县| 庐江县| 将乐县| 沂源县| 磐石市|