我目前正在以字符串形式从客户端计算机接收数据,但我希望它是[25,3]的2D数组。因此,我首先将其拆分为单个数组,然后尝试将该单个数组拆分为2D数组。但是,当我运行代码时,我不断看到一个空数组,有人知道为什么吗?
private void ClientReceiveData(object sender, ConnectedClient.NetDataEventArgs e)
{
if (string.IsNullOrEmpty(e.Message) == false)
{
if (e.ID == 0)
{
result = Array.ConvertAll(e.Message.Split(new[] { ',', }, StringSplitOptions.RemoveEmptyEntries), Double.Parse);
for (int i = 0; i < result.Length; i++)
{
resultarray2D[i % 3, i % 3] = result[i];
Trace.WriteLine(resultarray2D);
}
}
if (e.ID == 1)
{
answer = Array.ConvertAll(e.Message.Split(new[] { ',', }, StringSplitOptions.RemoveEmptyEntries), Double.Parse);
for (int i = 0; i < answer.Length; i++)
{
answerarray2D[i % 3, i / 3] = answer[i];
Trace.WriteLine(answerarray2D);
}
}
答案 0 :(得分:1)
我一直看到一个空数组
否,您会看到数组上ToString
的默认输出是什么,它是类的名称。如果要查看数组的内容,则需要遍历并输出各个项目:
int rowLength = arr.GetLength(0);
int colLength = arr.GetLength(1);
for (int i = 0; i < rowLength; i++)
{
for (int j = 0; j < colLength; j++)
{
Console.Write(string.Format("{0} ", arr[i, j]));
}
Console.WriteLine();
}
答案 1 :(得分:1)
这应该有效(我对上述人员的内容进行了一些更改,并在您的代码中实现了该功能):
if (string.IsNullOrEmpty(e.Message) == false)
{
if (e.ID == 0)
{
result = Array.ConvertAll(e.Message.Split(new[] { ',', }, StringSplitOptions.RemoveEmptyEntries), Double.Parse);
for (int x = 0; x < result.Length; x++)
{
resultarray2D[x / 3, x % 3] = result[x];
}
int rowLength = resultarray2D.GetLength(0);
int colLength = resultarray2D.GetLength(1);
for (int i = 0; i < rowLength; i++)
{
for (int j = 0; j < colLength; j++)
{
Trace.WriteLine(string.Format("{0} ", resultarray2D[i, j]));
}
Trace.WriteLine("\n");
}
}