Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#-将字符串数组转换为多维字符串数组_C#_Arrays_Multidimensional Array - Fatal编程技术网

C#-将字符串数组转换为多维字符串数组

C#-将字符串数组转换为多维字符串数组,c#,arrays,multidimensional-array,C#,Arrays,Multidimensional Array,我试图将一个36个字符的字符串数组放入一个6x6多维字符串数组中,但在如何实现这一点上,我遇到了很多困难 这是我的字符串: public static readonly string[] Supported = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s

我试图将一个36个字符的字符串数组放入一个6x6多维字符串数组中,但在如何实现这一点上,我遇到了很多困难

这是我的字符串:

public static readonly string[] Supported = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };
我想将其放入这个数组,但要以编程方式:

string[,] grid = new string[,] 
        {
            { "0", "1", "2", "3", "4", "5" },
            { "6", "7", "8", "9", "a", "b" },
            { "c", "d", "e", "f", "g", "h" },
            { "i", "j", "k", "l", "m", "n" },
            { "o", "p", "q", "r", "s", "t" },
            { "u", "v", "w", "x", "y", "z" }
        };

这样做现实可行/简单吗?

试试这样的方法:

int width = 6;
string[,] grid = new string[width,width];
for(int i = 0; i < Supported.Length; i++)
{
    grid[i / width, i % width] = Supported[i];
}
class Program
{
    static void Main(string[] args)
    {
        string[] strsArray = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };

        string[,] strsMatrix = ConvertStrsArrayToStrsMatrix(strsArray, 6);
    }

    private static string[,] ConvertStrsArrayToStrsMatrix(string[] strsArray, int maxAmountRow)
    {
        int max = strsArray.Length / maxAmountRow;
        string[,] strsMatrix = new string[max, maxAmountRow];
        int counter = 0;
        for(int row = 0; row < max; row++)
        {
            for(int column = 0; column < maxAmountRow; column++)
            {
                strsMatrix[row, column] = strsArray[counter];
                counter++;
            }
        }

        return strsMatrix;
    }
}
int-width=6;
字符串[,]网格=新字符串[宽度,宽度];
for(int i=0;i

正如@DragAndDrop所指出的,
intwidth=6
仅适用于您给出的特定示例。我把它作为一个变量,而不是在
网格
初始化中硬编码
6
,以显示这些值可以调整(如果矩阵不是平方的,则留出空间)。您应该找到一种查找值的方法(即
MathSqrt(Supported.Length)
)。此外,网格上没有边界检查,请确保在计算网格的长度和宽度时,我会这样做:

int width = 6;
string[,] grid = new string[width,width];
for(int i = 0; i < Supported.Length; i++)
{
    grid[i / width, i % width] = Supported[i];
}
class Program
{
    static void Main(string[] args)
    {
        string[] strsArray = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };

        string[,] strsMatrix = ConvertStrsArrayToStrsMatrix(strsArray, 6);
    }

    private static string[,] ConvertStrsArrayToStrsMatrix(string[] strsArray, int maxAmountRow)
    {
        int max = strsArray.Length / maxAmountRow;
        string[,] strsMatrix = new string[max, maxAmountRow];
        int counter = 0;
        for(int row = 0; row < max; row++)
        {
            for(int column = 0; column < maxAmountRow; column++)
            {
                strsMatrix[row, column] = strsArray[counter];
                counter++;
            }
        }

        return strsMatrix;
    }
}
类程序
{
静态void Main(字符串[]参数)
{
字符串[]strArray={“0”、“1”、“2”、“3”、“4”、“5”、“6”、“7”、“8”、“9”、“a”、“b”、“c”、“d”、“e”、“f”、“g”、“h”、“i”、“j”、“k”、“l”、“m”、“n”、“o”、“p”、“q”、“r”、“s”、“t”、“u”、“v”、“w”、“x”、“y”、“z”};
字符串[,]strsMatrix=convertstrarraytostrsmatrix(strarray,6);
}
私有静态字符串[,]convertstrsarytostrsmatrix(字符串[]strsArray,int-maxamontraw)
{
int max=strArray.Length/maxamontraw;
字符串[,]strsMatrix=新字符串[max,maxAmountRow];
int计数器=0;
对于(int row=0;row
如果您想支持可能的过度行为:

class Program
{
    static void Main(string[] args)
    {
        string[] strsArray = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "mustafa" };

        string[,] strsMatrix = ConvertStrsArrayToStrsMatrix(strsArray, 6);
    }

    private static string[,] ConvertStrsArrayToStrsMatrix(string[] strsArray, int maxAmountRow)
    {
        int max = strsArray.Length / maxAmountRow;
        if(max * maxAmountRow < strsArray.Length)
        {
            max++;
        }
        string[,] strsMatrix = new string[max, maxAmountRow];
        int counter = 0;
        for(int row = 0; row < max; row++)
        {
            for(int column = 0; column < maxAmountRow; column++)
            {
                if (counter < strsArray.Length)
                {
                    strsMatrix[row, column] = strsArray[counter];
                    counter++;
                }
                else
                {
                    break;
                }
            }
        }

        return strsMatrix;
    }
}
类程序
{
静态void Main(字符串[]参数)
{
字符串[]strArray={“0”、“1”、“2”、“3”、“4”、“5”、“6”、“7”、“8”、“9”、“a”、“b”、“c”、“d”、“e”、“f”、“g”、“h”、“i”、“j”、“k”、“l”、“m”、“n”、“o”、“p”、“q”、“r”、“s”、“t”、“u”、“v”、“w”、“x”、“y”、“z”、“mustafa”};
字符串[,]strsMatrix=convertstrarraytostrsmatrix(strarray,6);
}
私有静态字符串[,]convertstrsarytostrsmatrix(字符串[]strsArray,int-maxamontraw)
{
int max=strArray.Length/maxamontraw;
if(最大*最大长度
如果您
Math.天花(Math.Sqrt(Supported.Length))
,这将给出矩阵的大小(均匀或不均匀矩阵)

从支持的文件中删除第一个元素:

1 2 3 4 5 6
7 8 9 a b c
d e f g h i
j k l m n o
p q r s t u
v w x y z
将“A”添加到原始数组的开头

A 0 1 2 3 4 5
6 7 8 9 a b c
d e f g h i j
k l m n o p q
r s t u v w x
y z
方法(1):

0,1,2,3,4,5
6,7,8,9,a,b
c,d,e,f,g,h
i,j,k,l,m,n
o,p,q,r,s,t
u,v,w,x,y,z
以下代码还可用于将支持的数组转换为二维数组并打印二维数组

    public static readonly string[] Supported = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };
    static void Main(string[] args)
    {
        int noOfCols = 6;//your own choice to split w.r.t columns
        int noOfRows=Supported.Length/noOfCols;
        string[,] array2D=new string[noOfRows,noOfCols];
        int n = 0, m = 0;
        for(int i=0;i<Supported.Length;i++)
        {
            array2D[n, m] = Supported[i];
            m++;
            if((m=m%noOfCols)==0)
            {
                n++;
            }
        }
    }
这是thread1和thread2的两种方法

static void FillFromZeroToMidIndex(string[,] array2D,int midIndex,int noOfCols)
    {
        int n = 0, m = 0;
        for (int i = 0; i<=midIndex; i++)
        {
            array2D[n, m] = Supported[i];
            m++;
            if ((m = m % noOfCols) == 0)
            {
                n++;
            }
        }
    }
    static void FillFromLastToMidUpIndex(string[,] array2D, int midUpIndex, int lastIndex, int noOfCols, int noOfRows)
    {
        int n = noOfRows-1, m = noOfCols-1;
        for (int i = lastIndex; i >= midUpIndex; i--)
        {
            array2D[n, m] = Supported[i];
            m--;
            if (m<0)
            {
                m = noOfCols-1;
                n--;
            }
        }
    }

很有可能,先试:-)可能的重复和相关。您的问题只是一个特定的案例,将列表拆分成n个大小的束。回答为两种可能的行为处理方式您定义了magic int 6,在数学中使用它,这样改变一个会改变另一个。并且要小心越界。@拖放:我们必须使用显式
6
(除非矩阵是平方一):
36=2*18=3*12=…=6*6
@dragandrop correct,
int 6
是这样定义的,只是因为OP的矩阵是6x6,我将编辑我的答案以解决您的一些评论:-)@DmitryBychenko,
I/width
相当于
I/6
。和
支持。长度
width²

        for(int i=0;i<noOfRows;i++)
        {
            for(int j=0;j<noOfCols;j++)
            {
                Console.Write(array2D[i,j]);
                if(j<noOfCols-1)
                {
                    Console.Write(",");
                }
            }
            Console.WriteLine();
        }
0,1,2,3,4,5
6,7,8,9,a,b
c,d,e,f,g,h
i,j,k,l,m,n
o,p,q,r,s,t
u,v,w,x,y,z