如何水平打印数组的内容? [英] How can I print the contents of an array horizontally?

查看:28
本文介绍了如何水平打印数组的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么控制台窗口不水平而不是垂直打印数组内容?

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

有办法改变吗?

如何使用 Console.WriteLine() 水平而不是垂直显示数组的内容?

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

例如:

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

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

推荐答案

您可能正在使用 Console.WriteLine 来打印数组.

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());
}

如果您不想将每个项目都放在单独的行上,请使用 Console.Write:

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());
}

string.Join(在 .NET Framework 4 或更高版本中):

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

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

这篇关于如何水平打印数组的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆