简体   繁体   English

C# - 2D数组字符串数组

[英]C# - 2D Array of String Arrays

I am trying to create a world map for a console-based game. 我正在尝试为基于控制台的游戏创建世界地图。

In the World class I have string arrays that can be printed using a level print method. 在World类中,我有可以使用级别打印方法打印的字符串数组。

public static string[] A1 = new string[]
{
    (" level data"),
    (" level data"),
};

This works if I do 如果我这样做的话

Render.DrawLevel(World.A1);

where Render.DrawLevel takes args: 其中Render.DrawLevel采用args:

(string[] _level)

However, in the World Class, I want to create a two-dimensional array that can hold all of the Map's string arrays. 但是,在World Class中,我想创建一个可以容纳所有Map的字符串数组的二维数组。

I have tried: 我试过了:

public static string[,][] Map = new string[,][]
{
    { A1 , A2 },
    { B1 , B2 },
};

But if I try to use: 但如果我尝试使用:

World.Map[0,0]

as my arguments for level printed, I get given an error that this is NULL , instead of the string[] A1. 作为我打印水平的参数,我得到一个错误,它是NULL ,而不是字符串[] A1。

How can I create a 2d array of arrays, and refer to it properly for string[] arguments? 如何创建二维数组数组,并正确引用string []参数?

Thank you very much, 非常感谢你,

Fin. 鳍。

EDIT: Incorrectly copied code as 'static void' not just 'static'. 编辑:错误地将代码复制为'static void'而不仅仅是'static'。

Place the Map initialization after the initialization of the arrays. 在数组初始化之后放置Map初始化。 A1, A2, B1, B2 are equal to null during the initialization of Map . Map的初始化期间,A1,A2,B1,B2等于null。

The syntax is perfectly fine. 语法非常好。

This works as intended: 这按预期工作:

    public static string[] A1 = new string[]
        {
            (" level data 1"),
            (" level data 2"),
        };

    public static string[] A2 = new string[]
        {
            (" level data 1"),
            (" level data 2"),
        };

    public static string[] B1 = new string[]
        {
            (" level data 1"),
            (" level data 2"),
        };

    public static string[] B2 = new string[]
        {
            (" level data 1"),
            (" level data 2"),
        };

    public static string[,][] Map = new string[,][]
    {
        { A1, A2 },
        { B1, B2 },
    };

A two dimensional array is essentially an array of equally sized arrays. 二维阵列本质上是具有相同大小的阵列的阵列。 You can visualize it as a matrix, with each column (or row, doesn't really matter) as a separate array. 您可以将其可视化为矩阵,每列(或行,并不重要)作为单独的数组。 So then the elements in the first row (column) would be the first elements of each of the arrays. 那么第一行(列)中的元素将是每个数组的第一个元素。 What you are doing is trying to create a two dimensional array of arrays. 你正在做的是尝试创建一个二维数组数组。

The best answer on this is a great read concerning your question. 对最佳答案是关于你的问题一个伟大的阅读。

Your signature for Map is incorrect, it can't be void and a multi-dimensional string array. 您对Map的签名不正确,它不能为void和多维字符串数组。 Change it to: 将其更改为:

    public static string[,][] Map = new string[,][]
        {
            {A1,A2 },
            {B1,B2 },
        };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM