此数组初始化不正确吗? [英] Is this array initialization incorrect?

查看:170
本文介绍了此数组初始化不正确吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public byte[][,] Shapes = 
    {
        {
            {1,1}, 
            {1,1}
        }, 
        {
            {1}, 
            {1}, 
            {1}, 
            {1}
        },
        {
            {0,0,1},
            {1,1,1}
        }
    };

我收到此错误:
数组初始值设定项只能在变量或字段中使用初始化程序。尝试使用新表达式。

I get this error: "Array initializers can only be used in a variable or field initializer. Try using a new expression instead."

我可以这样做...

public class Shape
{
    public byte[][,] Shapes;

    public Shape()
    {
        Shapes = new byte[3][,];

        Shapes[0] = new byte[2, 2];
        Shapes[0][0, 0] = 1;
        Shapes[0][0, 1] = 1;
        Shapes[0][1, 0] = 1;
        Shapes[0][1, 1] = 1;

        Shapes[1] = new byte[1, 4];
        Shapes[1][0, 0] = 1;
        Shapes[1][0, 1] = 1;
        Shapes[1][0, 2] = 1;
        Shapes[1][0, 3] = 1;
    }
}

但这很难添加更多形状我的程序。

But that makes it very hard to add more shapes to my program.

初始化器错误吗?如果不允许这样做,最简单的方法是什么?

Is my initializer wrong? And if I'm not allowed to do it this way, what's the easiest way to set it out?

推荐答案

我:

  public byte[][,] Shapes = new byte[3][,]
    {
        new byte[,] { {1,1}, {1,1} },
        new byte[,] { {1}, {2}, {3}, {4} },
        new byte[,] { {0,0,1}, {1,1,1} }
    };

这篇关于此数组初始化不正确吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆