当前位置:网站首页>C#如何打印输出原版数组
C#如何打印输出原版数组
2022-07-01 06:20:00 【SteveDraw】
前言:
数组是一个非常重要的数据结构,我们经常会使用到它,可能最长用到的是遍历并打印它们,但是有个小问题我们边遍历边打印,往往会外形不会像原本的结构!
- 以一个数组为例,该数组结构例如:
int [] b= new int []{
1,2,3}
- 以调用换行打印函数为例(单行打印也一样,只是为了说明情况),打印输入如下:
1
2
3
这样我们如果同时使用其他数组结构并打印到CLI上就会影响判断和阅读,这样看来我们打印时需要还原成数组在代码定义时的模样!
解决方法
我们需要写一个专门输出特定数组类型的打印函数才行!Easy!直接上代码!
(1)函数方法
static void PrintArray(int[] a)//具体数组类型可根据实际更改
{
//需要对输入数组判断是否为空
if (a.Length != 0)
{
string str = "";
foreach (var i in a)
{
str = str + i + ",";
}
WriteLine("[" + str.Substring(0, str.Length - 1) + "]");//通过Substring()去除对应字符
}
if (a.Length == 0) WriteLine("[]");//如果数组为空,打印空数组格式即可
}
如果经常使用,在类库项目中,将函数封装成类方法,那将会优化你的效率!
(2)完整代码(附带测试代码):
using static System.Console;
namespace ConsoleApp1
{
class Program
{
static void PrintArray(int[] a)//具体数组类型可根据实际更改
{
//需要对输入数组判断是否为空
if (a.Length != 0)
{
string str = "";
foreach (var i in a)
{
str = str + i + ",";
}
WriteLine("[" + str.Substring(0, str.Length - 1) + "]");//通过Substring()去除对应字符
}
if (a.Length == 0) WriteLine("[]");//如果数组为空,打印空数组格式即可
}
static void Main(string[] args)
{
int[] a=new int[] {
1,2,3};
PrintArray(a);
ReadKey();
}
}
}
- 输出如下:
[1,2,3]
最后文中若有不足,还请多多勘误指正!
边栏推荐
- To sort out the anomaly detection methods, just read this article!
- 69 cesium code datasource loading geojson
- 【自动化运维】自动化运维平台有什么用
- Pychart configuring jupyter
- Using Baidu map to query national subway lines
- idea 好用插件汇总!!!
- Redis安装到Windows系统上的详细步骤
- 【ManageEngine卓豪】助力黄石爱康医院实现智能批量化网络设备配置管理
- [automatic operation and maintenance] what is the use of the automatic operation and maintenance platform
- three.js小结
猜你喜欢

High order binary search tree

Application of IT service management (ITSM) in Higher Education

C语言课设图书信息管理系统(大作业)

【#Unity Shader#自定义材质面板_第二篇】

DHT11 temperature and humidity sensor

C语言课设职工信息管理系统(大作业)

让厦门灌口镇田头村变“甜头”村的特色农产品之一是

SystemVerilog learning-08-random constraints and thread control
![[self use of advanced mathematics in postgraduate entrance examination] advanced mathematics Chapter 1 thinking map in basic stage](/img/54/f187e22ad69f3985d30376bad1fa03.png)
[self use of advanced mathematics in postgraduate entrance examination] advanced mathematics Chapter 1 thinking map in basic stage
![[enterprise data security] upgrade backup strategy to ensure enterprise data security](/img/59/e44c6533aa546e8854ef434aa64113.png)
[enterprise data security] upgrade backup strategy to ensure enterprise data security
随机推荐
libpng12.so. 0: cannot open shared object file: no such file or directory
golang panic recover自定义异常处理
SQL中DML语句(数据操作语言)
async 与 await
异常检测方法梳理,看这篇就够了!
阿里OSS Postman Invalid according to Policy: Policy Condition failed: [“starts-with“, “$key“, “test/“]
HCM Beginner (I) - Introduction
SOE spatial analysis server MySQL and PostGIS geospatial database of Postgres anti injection attack
DHT11 temperature and humidity sensor
Redis安装到Windows系统上的详细步骤
[ManageEngine Zhuohao] helps Huangshi Aikang hospital realize intelligent batch network equipment configuration management
Teach you how to implement a deep learning framework
子类调用父类的同名方法和属性
68 Cesium代码datasource加载czml
JMM details
【ITSM】什么是ITSM,IT部门为什么需要ITSM
Treasure taking from underground palace (memory based deep search)
B-tree series
kubeadm搭建kubenetes 集群(个人学习版)
ManageEngine Zhuohao helps you comply with ISO 20000 standard (IV)