如何横向打印数组的内容是什么?数组、横向、内容

2023-09-02 20:44:55 作者:青楼满、看寂寞

为什么没有控制台窗口打印数组内容横向而不是纵向?

有没有办法来改变这种状况?

我怎样才能显示我的数组的含量水平而不是垂直,用 Console.WriteLine()

例如:

  INT []号=新INT [100]
对于(INT I; I< 100;我++)
{
    号码[我] =我;
}

对于(INT I; I< 100;我++)
{
    Console.WriteLine(数字[I]);
}
 

解决方案 用二维数组打印一个杨辉三角

您可能正在使用 Console.WriteLine 打印阵列。

  INT []数组=新INT [] {1,2,3};
的foreach(数组VAR项)
{
    Console.WriteLine(item.ToString());
}
 

如果你不希望有一个单独的行使用 Console.Write 每一个项目:

  INT []数组=新INT [] {1,2,3};
的foreach(数组VAR项)
{
    Console.Write(item.ToString());
}
 

的string.join< T> (在.NET框架4或更高版本):

  INT []数组=新INT [] {1,2,3};
Console.WriteLine(的string.join(,,阵列));
 

Why doesn't the console window print the array contents horizontally rather than vertically?

Is there a way to change that?

How can I display the content of my array horizontally instead of vertically, with a Console.WriteLine()?

For example:

int[] numbers = new int[100]
for(int i; i < 100; i++)
{
    numbers[i] = i;
}

for (int i; i < 100; i++) 
{
    Console.WriteLine(numbers[i]);
}

解决方案

You are probably using Console.WriteLine for printing the array.

int[] array = new int[] { 1, 2, 3 };
foreach(var item in array)
{
    Console.WriteLine(item.ToString());
}

If you don't want to have every item on a separate line use Console.Write:

int[] array = new int[] { 1, 2, 3 };
foreach(var item in array)
{
    Console.Write(item.ToString());
}

or string.Join<T> (in .NET Framework 4 or later):

int[] array = new int[] { 1, 2, 3 };
Console.WriteLine(string.Join(",", array));