我正在尝试将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" }
};
实际上可行/简单吗?
答案 0 :(得分:4)
尝试这样的事情:
int width = 6;
string[,] grid = new string[width,width];
for(int i = 0; i < Supported.Length; i++)
{
grid[i / width, i % width] = Supported[i];
}
正如@DragAndDrop指出的那样,int width = 6
仅适用于您给出的特定示例。我将其保留为变量,而不是在6
初始化中对grid
进行硬编码,以表明可以调整这些值(并在矩阵不为正方形的情况下留出空间)。您应该找出一种找到值的方法(即MathSqrt(Supported.Length)
)。另外,grid
上没有边界检查,在计算grid
的{{1}}和length
的值时,请确保包括边界。
答案 1 :(得分:1)
如果您Math.Ceiling(Math.Sqrt(Supported.Length))
,将为您提供矩阵应有的大小(均匀或不均匀的矩阵)。
using System;
public class Program
{
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" };
public static void Main()
{
int matrixSize = (int)Math.Ceiling(Math.Sqrt(Supported.Length));
string[,] matrix = new string[matrixSize, matrixSize];
for (int i = 0; i < Supported.Length; i++)
{
matrix[i / matrixSize, i % matrixSize] = Supported[i];
}
for (int i = 0; i < matrix.GetLength(0); i++)
{
for (int j = 0; j < matrix.GetLength(1); j++)
{
Console.Write("{0} ", matrix[i, j]);
}
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
从“受支持”中删除第一个元素:
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
答案 2 :(得分:0)
我会做这样的事情:
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;
}
}
如果您想支持可能的超出限额:
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;
}
}
答案 3 :(得分:0)
方法(1):
以下代码也可以用于将Supported数组转换为2维数组并打印2D数组。
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++;
}
}
}
方法(2): 我们还可以使用多线程过程在2D数组中从受支持数组的开始到中间点以及最后到中间点的下一个相邻点并行分配值,但是请记住,这种机制将在我们拥有大量数组的情况下使用单个数组或数组列表中的数据,因此您可以将其做成小块,并可以利用多线程将它们填充为并行线程。但是,我仅使用两个线程来填充此新线程方法。
Thread thread1 = new Thread(delegate(){
FillFromZeroToMidIndex(array2D, Supported.Length / 2,noOfCols);
});
Thread thread2 = new Thread(delegate()
{
FillFromLastToMidUpIndex(array2D, (Supported.Length / 2) + 1, Supported.Length - 1, noOfCols,noOfRows);
});
thread1.Start();
thread2.Start();
while (thread1.IsAlive || thread2.IsAlive) { }
这是线程1和线程2的两种方法
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--;
}
}
}
您还可以根据自己的想法制定自己的逻辑,这对于您选择自己想要的逻辑更好 保持祝福。
以下代码将用于打印2D阵列
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