当前位置:网站首页>C how to print out the original array
C how to print out the original array
2022-07-01 06:24:00 【SteveDraw】
Preface :
Array is a very important data structure , We often use it , Probably the longest used is to traverse and print them , But there is a small problem. We print while traversing , Often the shape will not be like the original structure !
- Take an array as an example , The array structure is for example :
int [] b= new int []{
1,2,3}
- Take calling the newline print function as an example ( The same goes for single line printing , Just to illustrate the situation ), Print the input as follows :
1
2
3
So if we use other array structures at the same time and print to CLI It will affect judgment and reading , In this way, we need to restore the array to its appearance at the time of code definition when printing !
resolvent
We need to write a print function that outputs a specific array type !Easy! Go straight to the code !
(1) Function method
static void PrintArray(int[] a)// The specific array type can be changed according to the actual situation
{
// You need to judge whether the input array is empty
if (a.Length != 0)
{
string str = "";
foreach (var i in a)
{
str = str + i + ",";
}
WriteLine("[" + str.Substring(0, str.Length - 1) + "]");// adopt Substring() Remove the corresponding characters
}
if (a.Length == 0) WriteLine("[]");// If the array is empty , Print the empty array format
}
If you use , In a class library project , Encapsulate functions into class methods , That will optimize your efficiency !
(2) Complete code ( Attached test code ):
using static System.Console;
namespace ConsoleApp1
{
class Program
{
static void PrintArray(int[] a)// The specific array type can be changed according to the actual situation
{
// You need to judge whether the input array is empty
if (a.Length != 0)
{
string str = "";
foreach (var i in a)
{
str = str + i + ",";
}
WriteLine("[" + str.Substring(0, str.Length - 1) + "]");// adopt Substring() Remove the corresponding characters
}
if (a.Length == 0) WriteLine("[]");// If the array is empty , Print the empty array format
}
static void Main(string[] args)
{
int[] a=new int[] {
1,2,3};
PrintArray(a);
ReadKey();
}
}
}
- Output is as follows :
[1,2,3]
Finally, if there are deficiencies in the text , Please also make many corrections !
边栏推荐
- [self use of advanced mathematics in postgraduate entrance examination] advanced mathematics Chapter 1 thinking map in basic stage
- 异常检测方法梳理,看这篇就够了!
- High order binary search tree
- UOW of dev XPO comparison
- ManageEngine Zhuohao helps you comply with ISO 20000 standard (IV)
- ForkJoin和Stream流测试
- 【ManageEngine卓豪】移动终端管理解决方案,助力中州航空产业数字化转型
- The row and column numbers of each pixel of multi-source grid data in the same area are the same, that is, the number of rows and columns are the same, and the pixel size is the same
- HCM Beginner (III) - quickly enter pa70 and pa71 to browse employee information PA10
- SystemVerilog learning-08-random constraints and thread control
猜你喜欢

Distributed lock implementation
![[summary of knowledge points] chi square distribution, t distribution, F distribution](/img/a6/bb5cabbfffb0edc9449c4c251354ae.png)
[summary of knowledge points] chi square distribution, t distribution, F distribution

async 与 await

Excel visualization

Teach you how to implement a deep learning framework

High order binary search tree

Picture server project test

JMM details

数据库产生死锁了请问一下有没有解决办法
![[ITSM] what is ITSM and why does it department need ITSM](/img/e1/85b5f00f124829b6a6b40c5cf621bd.png)
[ITSM] what is ITSM and why does it department need ITSM
随机推荐
Promise
阶乘约数(唯一分解定理)
c# Xml帮助类
How does the port scanning tool help enterprises?
Tidb single machine simulation deployment production environment cluster (closed pit practice, personal test is effective)
Factorial divisor (unique decomposition theorem)
[ManageEngine Zhuohao] the role of LAN monitoring
手把手教你实现一个深度学习框架...
three. JS summary
自开发软件NoiseCreater1.1版本免费试用
扩散(多源广搜)
B-tree series
Although pycharm is marked with red in the run-time search path, it does not affect the execution of the program
网络爬虫
webapck打包原理--启动过程分析
SystemVerilog learning-07-class inheritance and package use
Make: g++: command not found
Solve the problem of garbled files uploaded by Kirin v10
让厦门灌口镇田头村变“甜头”村的特色农产品之一是
Treasure taking from underground palace (memory based deep search)