How to print the contents of an array horizontally?

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

Is there any way to change this?

How can I display the contents of my array horizontally, rather than vertically, with 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]); } 
+65
arrays c #
Sep 13 2018-10-13
source share
11 answers

You are probably using Console.WriteLine to print the array.

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

If you do not want each item on a separate line to 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)); 
+120
Sep 13 '10 at 12:45
source share

I would suggest:

 foreach(var item in array) Console.Write("{0}", item); 

As described above, an exception is raised except for it if one element is null .

 Console.Write(string.Join(" ", array)); 

would be ideal if the array is string[]

+28
Sep 13 '10 at 12:49
source share

Just swipe through the array and write the elements to the console using Write instead of WriteLine :

 foreach(var item in array) Console.Write(item.ToString() + " "); 

As long as your elements do not have line breaks, this will create one line.

... or, as John Skeet said, will provide a little more context to your question.

+18
Sep 13 2018-10-13
source share
 foreach(var item in array) Console.Write(item.ToString() + "\t"); 
+3
May 13 '15 at 5:27
source share
 namespace ReverseString { class Program { static void Main(string[] args) { string stat = "This is an example of code" + "This code has written in C#\n\n"; Console.Write(stat); char[] myArrayofChar = stat.ToCharArray(); Array.Reverse(myArrayofChar); foreach (char myNewChar in myArrayofChar) Console.Write(myNewChar); // Yuo just need to write the function Write instead of WriteLine Console.ReadKey(); } } } 



This is the conclusion.

 #C ni nettirw sah edoc sihTedoc fo elpmaxe na si sihT 
+2
Mar 08 '16 at 4:52
source share

If you need to prettyly print an array of arrays, something like this might work http://aaron-hoffman.blogspot.com/2013/11/pretty-print-array-of-arrays-in-net-c.html

 public string PrettyPrintArrayOfArrays(int[][] arrayOfArrays) { if (arrayOfArrays == null) return ""; var prettyArrays = new string[arrayOfArrays.Length]; for (int i = 0; i < arrayOfArrays.Length; i++) { prettyArrays[i] = "[" + String.Join(",", arrayOfArrays[i]) + "]"; } return "[" + String.Join(",", prettyArrays) + "]"; } 

Result:

 [[2,3]] [[2,3],[5,4,3]] [[2,3],[5,4,3],[8,9]] 
+1
Nov 25 '13 at 20:07
source share
 int[] n=new int[5]; for (int i = 0; i < 5; i++) { n[i] = i + 100; } foreach (int j in n) { int i = j - 100; Console.WriteLine("Element [{0}]={1}", i, j); i++; } 
+1
Mar 14 '14 at 7:43
source share
  public static void Main(string[] args) { int[] numbers = new int[10]; Console.Write("index "); for (int i = 0; i < numbers.Length; i++) { numbers[i] = i; Console.Write(numbers[i] + " "); } Console.WriteLine(""); Console.WriteLine(""); Console.Write("value "); for (int i = 0; i < numbers.Length; i++) { numbers[i] = numbers.Length - i; Console.Write(numbers[i] + " "); } Console.ReadKey(); } } } 
+1
Nov 27 '14 at 15:09
source share

Using Console.Write only works if the stream is the only stream written to the Console, otherwise your output may be interleaved with another output that may or may not insert new lines, as well as other unwanted characters. To keep your array printed intact, use Console.WriteLine to write one line. Most array objects can be printed horizontally (depending on the type of ToString ()) using a non-generic Join, available before .NET 4.0:

  int[] numbers = new int[100]; for(int i= 0; i < 100; i++) { numbers[i] = i; } //For clarity IEnumerable strings = numbers.Select<int, string>(j=>j.ToString()); string[] stringArray = strings.ToArray<string>(); string output = string.Join(", ", stringArray); Console.WriteLine(output); //OR //For brevity Console.WriteLine(string.Join(", ", numbers.Select<int, string>(j => j.ToString()).ToArray<string>())); 
0
Dec 20 '17 at 20:15
source share

Below is the simplest solution.

 Console.WriteLine("[{0}]", string.Join(", ", array)); 

Conclusion: [1, 2, 3, 4, 5]

Another short solution

 Array.ForEach(array, val => Console.Write("{0} ", val)); 

Conclusion: 1 2 3 4 5 , or, if you need to add, add,, use below

 int i = 0; Array.ForEach(array, val => Console.Write(i == array.Length -1) ? "{0}" : "{0}, ", val)); 

Conclusion: 1, 2, 3, 4, 5

0
Aug 14 '19 at 23:25
source share
 private int[,] MirrorH(int[,] matrix) //the method will return mirror horizintal of matrix { int[,] MirrorHorizintal = new int[4, 4]; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j ++) { MirrorHorizintal[i, j] = matrix[i, 3 - j]; } } return MirrorHorizintal; } 
-3
Nov 20 '16 at 5:49
source share



All Articles