当前位置:网站首页>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 !
边栏推荐
- Using Baidu map to query national subway lines
- make: g++:命令未找到
- Freeswitch dial the extension number
- SQL语句
- Pol8901 LVDS to Mipi DSI supports rotating image processing chip
- C语言课设工资管理系统(大作业)
- 端口扫描工具对企业有什么帮助?
- [ITSM] what is ITSM and why does it department need ITSM
- Servlet
- Arcserver password reset (account cannot be reset)
猜你喜欢
随机推荐
连续四年入选Gartner魔力象限,ManageEngine卓豪是如何做到的?
Arcserver password reset (account cannot be reset)
Solve the problem of garbled files uploaded by Kirin v10
Record currency in MySQL
网络爬虫
【KV260】利用XADC生成芯片温度曲线图
golang panic recover自定义异常处理
C语言课设学生选修课程系统(大作业)
三分钟带你快速了解网站开发的整个流程
Mysql 表分区创建方法
SystemVerilog learning-06-class encapsulation
【ManageEngine】如何实现网络自动化运维
[automatic operation and maintenance] what is the use of the automatic operation and maintenance platform
Restframework-simplejwt rewrite authentication mechanism
How does the port scanning tool help enterprises?
SystemVerilog learning-07-class inheritance and package use
Tidb single machine simulation deployment production environment cluster (closed pit practice, personal test is effective)
kubeadm搭建kubenetes 集群(个人学习版)
Functions of switch configuration software
异常检测方法梳理,看这篇就够了!









