C# 多维数组 交错数组的区别,即 [ , ] 与 [ ][ ]的区别 (转载)

多维数组的声明


在声明时,必须指定数组的长度,格式为 type [lenght ,lenght ,lengh, ... ]

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

或声明时即赋值,由系统推断长度

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

 

 

交错数组的声明


声明时,至少需要指定第一维的长度,格式为 type [ ] [ ] [ ] ...

int[][] test1 = new int[5][];

 

int [][] test1 = new int[][];    //注意,此的声明方式是错的

或者声明时即赋值,由系统推断长度

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

 

 

多维数组与交错数组 二者的相同、区别


两者声明时,都必须指定长度,多维数组必须指定每一维的长度,而交错数组需要至少需要指定第一维的长度。
多维数组声明时,符号是这样的 [ , , , , ],逗号在 方括号 [ ] 中,每一维长度用逗号分隔。而交错数组每一维独立在 [ ]中
当你想指定数组长度时,只能在等号右侧指定,int [,] test1 = new int [3,3] 是正确的 ;int [6,4] test1 = new int [6,4] 是错误的;
下面以代码形式说明

 

 

大小不一致的多维数组会发生错误

int [,] test1 = {
            {1,2,3,4},
            {1,2,3},
            {1,2}
        };         //这样是错的,长度必须一致

 

int [,] test1 = new int [4,5] {
            {1,2,3,4,5},
            {1,2,3},
            {1,2,3}
        };        //这样也是错误的,长度必须一致,必须为每一个位置赋值

这一点C#与C语言有所区别,C语言可以不全赋值,没有赋值的位置系统默认为0。

 

下面的方法是正确的

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

 

 

初始化交错数组

上面已经说了声明一个交错数组的方法

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[],这正是交错数组的特性。交错数组是由数组构成的数组,交错数组要求为内部的每个数组都创建实例。

即交错数组的每一维都是一个实例,每一个实例为一个数组。

 

 

数组的长度是固定的

无论多维数组还是交错数组,长度都是固定的,不能随意改变(应该是指不能像List一样,随意增加和删除元素)。

 

 

获取数组的长度

使用 对象.Length 获取数组的长度,需要注意的是,多维数组的长度是每一维相乘,即元素总个数。

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

 

而交错数组的长度则是“内部组成的数组的个数”,例如

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

 

 

方法

多维数组、交错数组的方法无差别,都具有Sort()、Clear()等方法,这里不再赘述,关于数组的高级用法,请查阅
https://www.jb51.net/Special/265.htm

 

下列为System.Array类的属性

由于系统提供的方法比较多,有兴趣请查阅

https://docs.microsoft.com/zh-cn/dotnet/api/system.array

 

 

使用数组初始化类型


在C#中有 lambda、匿名类等等,C# 5.0/6.0 后,给声明类、声明类型类型、赋值等有了很方便的操作方法。下面举例测试。

 

 

例子1

使用数组对集合、集合泛型等初始化
声明一个 List 泛型集合

using System;
using System.Collections.Generic;

namespace NetCore
{
    class Program
    {
        //main中的代码
        static void Main(string[] args)
        {
            List<string> list = new List<string>();

            Console.ReadKey();
        }
    }
}

 

那么,给集合 list 增加一个项,用 Add() 方法

using System;
using System.Collections.Generic;

namespace NetCore
{
    class Program
    {
        //main中的代码
        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();
        }
    }
}

 

利用 “数组” 来添加新项

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

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

//以上两种方法都可以,注意后面有没有()

 

 

例子2

上面的例子利用数组直接在集合声明时初始化,但是不能很好的声明“骚操作”。

 

试试交错数组

1,在 program 类 所在的命名空间中写一个类

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();
}

 

完整代码如下

using System;
using System.Collections.Generic;

namespace NetCore
{
    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();
        }
    }
}

 

由于类是引用类型,它的内存是引用地址,不像 int、char等类型,所以在对类(引用类型)使用数组、集合等形式时,可以用 “交错数组” 来理解。

 

 

原文链接

 

转载于:https://www.cnblogs.com/OpenCoder/p/11151247.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值