当前位置:网站首页>C#如何打印輸出原版數組
C#如何打印輸出原版數組
2022-07-01 06:24: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]
最後文中若有不足,還請多多勘誤指正!
边栏推荐
- Kubedm builds kubenetes cluster (Personal Learning version)
- HCM Beginner (II) - information type
- JDBC connection pool
- Make: g++: command not found
- High order binary search tree
- 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
- [ManageEngine Zhuohao] helps Huangshi Aikang hospital realize intelligent batch network equipment configuration management
- C语言课设销售管理系统设计(大作业)
- SQL语句
- C语言课设图书信息管理系统(大作业)
猜你喜欢

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

高阶-二叉平衡树

Picture server project test
![[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

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

让田头村变甜头村的特色农产品是仙景芋还是白菜

记磁盘扇区损坏导致的Mysql故障排查

SystemVerilog learning-08-random constraints and thread control

Tidb database characteristics summary

68 Cesium代码datasource加载czml
随机推荐
[ManageEngine Zhuohao] mobile terminal management solution, helping the digital transformation of Zhongzhou aviation industry
C语言课设学生考勤系统(大作业)
地宮取寶(記憶化深搜)
Elements of database ER diagram
让厦门灌口镇田头村变甜头村的特色农产品之一是蚂蚁新村
SystemVerilog learning-06-class encapsulation
[postgraduate entrance examination advanced mathematics Wu Zhongxiang +880 version for personal use] advanced mathematics Chapter II Basic Stage mind map
Dongle data collection
ArcServer密码重置(账号不可以重置)
C语言课设工资管理系统(大作业)
[ManageEngine Zhuohao] the role of LAN monitoring
Mysql 表分区创建方法
Golang panic recover custom exception handling
Make: g++: command not found
B-树系列
Redis安装到Windows系统上的详细步骤
Self confidence is indispensable for technology
High order binary balanced tree
C语言课设学生选修课程系统(大作业)
Save data in browser to local file