如何横向打印数组的内容是什么? [英] How to print contents of array horizontally?

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

问题描述

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

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

我怎样才能显示我的数组的含量水平而不是垂直,用 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));

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

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