当前位置:网站首页>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]
最后文中若有不足,还请多多勘误指正!
边栏推荐
- Mysql 表分区创建方法
- kotlin位运算的坑(bytes[i] and 0xff 报错)
- Recueillir des trésors dans le palais souterrain (recherche de mémoire profonde)
- 10 golang operator
- 扩散(多源广搜)
- 【ManageEngine卓豪】网络运维管理是什么,网络运维平台有什么用
- 自开发软件NoiseCreater1.1版本免费试用
- 【#Unity Shader#自定义材质面板_第二篇】
- 【ManageEngine卓豪】移动终端管理解决方案,助力中州航空产业数字化转型
- Pychart configuring jupyter
猜你喜欢

HCM Beginner (I) - Introduction

自开发软件NoiseCreater1.1版本免费试用

IT服务管理(ITSM)在高等教育领域的应用

Freeswitch dial the extension number

idea 好用插件汇总!!!

FPGA - clocking -02- clock wiring resources of internal structure of 7 Series FPGA
![[automatic operation and maintenance] what is the use of the automatic operation and maintenance platform](/img/14/756d566744d6e4a988a284c5b30130.png)
[automatic operation and maintenance] what is the use of the automatic operation and maintenance platform

B-tree series

C语言课设学生选修课程系统(大作业)

MongoDB:一、MongoDB是什么?MongoDB的优缺点
随机推荐
异常检测方法梳理,看这篇就够了!
码力十足学量化|如何在财务报告寻找合适的财务公告
Top 10 Free 3D modeling software for beginners in 2022
Kubedm builds kubenetes cluster (Personal Learning version)
记磁盘扇区损坏导致的Mysql故障排查
69 cesium code datasource loading geojson
Uniapp tree level selector
Save data in browser to local file
HCM Beginner (III) - quickly enter pa70 and pa71 to browse employee information PA10
SQL中DML语句(数据操作语言)
JMM details
指数法和Random Forest实现山东省丰水期地表水体信息
ManageEngine Zhuohao helps you comply with ISO 20000 standard (IV)
kubeadm搭建kubenetes 集群(个人学习版)
three.js小结
浅谈SIEM
地宫取宝(记忆化深搜)
Dongle data collection
局域网监控软件有哪些功能
数据库产生死锁了请问一下有没有解决办法