当前位置:网站首页>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]
最后文中若有不足,还请多多勘误指正!
边栏推荐
- 阶乘约数(唯一分解定理)
- C语言课设职工信息管理系统(大作业)
- 记磁盘扇区损坏导致的Mysql故障排查
- Recueillir des trésors dans le palais souterrain (recherche de mémoire profonde)
- HCM Beginner (I) - Introduction
- 阿里OSS Postman Invalid according to Policy: Policy Condition failed: [“starts-with“, “$key“, “test/“]
- 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
- 连续四年入选Gartner魔力象限,ManageEngine卓豪是如何做到的?
- Picture server project test
- Teach you how to implement a deep learning framework
猜你喜欢
[self use of advanced mathematics in postgraduate entrance examination] advanced mathematics Chapter 1 thinking map in basic stage
JDBC database operation
Forkjoin and stream flow test
SQL语句
【ITSM】什么是ITSM,IT部门为什么需要ITSM
JMM details
Understanding of C manualresetevent class
Excel visualization
C语言课设职工信息管理系统(大作业)
three. JS summary
随机推荐
证券类开户有什么影响 开户安全吗
async 与 await
C语言课设工资管理系统(大作业)
端口扫描工具是什么?端口扫描工具有什么用
图片服务器项目测试
Differences between in and exists in MySQL
Picture server project test
自开发软件NoiseCreater1.1版本免费试用
XAF Bo of dev XPO comparison
子类调用父类的同名方法和属性
Pol8901 LVDS to Mipi DSI supports rotating image processing chip
Tidb single machine simulation deployment production environment cluster (closed pit practice, personal test is effective)
【ITSM】什么是ITSM,IT部门为什么需要ITSM
[ManageEngine Zhuohao] helps Huangshi Aikang hospital realize intelligent batch network equipment configuration management
libpng12.so. 0: cannot open shared object file: no such file or directory
Discrimination between left and right limits of derivatives and left and right derivatives
Projects and dependencies in ABP learning solutions
Redis安装到Windows系统上的详细步骤
三分钟带你快速了解网站开发的整个流程
On siem