当前位置:网站首页>C array
C array
2022-04-23 06:24:00 【Wood acridine】
C# Array
Characteristics of array :
The values stored in an array are of the same data type .
A specified element in an array is accessed through an index .
All arrays consist of contiguous memory locations .
Arrays are indexed from zero , That is, the array index starts from zero .
One dimensional array
grammar :
Define an array : data type [ ] Array name ;
initialization Elements in an array :
- data type [ ] Array name =new data type [ length ];
example :int[] intArr = new int[5]; The length of the array 5 The content of each element is not specified , Not initialized
- data type [ ] Array name ={ value 1, value 2,……}
example :int[] intArr1 = { 1, 2, 3, 4 }; Initializing arrays directly , The length of the array is calculated by the compiler . And initialize
- data type [ ] Array name =new data type [ length ] { value 1, value 2,……}
example :int[] intArr2 = new int[3] { 5, 6,7}; Indicates the length of the array 3, And initialize
Two dimensional array :
grammar :
data type [ ,] Array name ;
Create a two-dimensional array and initialize it :
data type [ ,] Array name =new data type [ m,n]{ { ,},{ ,}};
example :int[,] intDArr = new int[2, 2] { { 1, 2 }, { 3, 4 } };
foreach Traverse
grammar :foreach( data type Variable name in Array name ){ Sentence block ;}
Traversing a one-dimensional array
example :int[] intArr1 = { 1, 2, 3, 4 };
foreach (int item in intArr1)
{
Console.WriteLine(item);
}
Traversing a two-dimensional array
example : int[,] intDArr = new int[2, 2] { { 1, 2 }, { 3, 4 } };
for (int i = 0; i < intDArr.GetLength(0); i++)
{
for (int j = 0; j < intDArr.GetLength(1); j++)
{
Console.Write("{0}\t", intDArr[i, j]);
}
Console.Write("\n"); Line break
}
notes :foreach Can only be used for arrays 、 String or collection data type
Type of structure
Structure type is a custom value type .
Definition grammar :
Access modifier struct Structure name { Structural members }
Structures and classes difference :
Structure :
It is allowed not to use new Exemplify it
There is no default constructor
Can't inherit class
Not allowed abstract、protected as well as sealed modification
class :
You have to use new Instantiation
There is a default construction method
Can inherit classes
Allow to use abstract、protected as well as sealed modification
Enumeration type
Enumeration type is a special value type
grammar :
Access modifier enum Variable name : data type { value 1, value 2,}
Enumeration class Type determines the maximum content that can be contained in an enumerated class .
data type : It refers to the data type of the value in the enumeration . Can only be integer type . Such as byte、short、int、long etc. .
example : enum Week : short
{
SUN, // Sunday
MON, // Monday
TUE, // Tuesday
WED, // Wednesday
THU, // Thursday
FRI, // Friday
SAT, // Saturday
UNDEFIN,
}
static void PrintWeek(Week week)
{
switch (week)
{
case Week.MON:// Monday
Console.WriteLine(" Monday ");
break;
case Week.TUE:// Tuesday
Console.WriteLine(" Tuesday ");
break;
case Week.WED:// Wednesday
Console.WriteLine(" Wednesday ");
break;
case Week.THU:// Thursday
Console.WriteLine(" Thursday ");
break;
case Week.FRI:// Friday
Console.WriteLine(" Friday ");
break;
case Week.SAT:// Saturday
Console.WriteLine(" Saturday ");
break;
case Week.SUN:// Sunday
Console.WriteLine(" Sunday ");
break;
}
}
版权声明
本文为[Wood acridine]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210616405278.html
边栏推荐
- Example of ticket selling with reentrant lock
- Linear algebra Chapter 1 - determinant
- Sakura substring thinking
- Exception handling: grab and throw model
- Create binary tree
- Substring Inversion (Easy Version)
- 11.a==b?
- Reading of denoising papers - [cvpr2022] blind2blind: self supervised image denoising with visible blind spots
- Chapter 4 of line generation - linear correlation of vector systems
- C language file operation
猜你喜欢
Contrôle automatique (version Han min)
Preparedstatement prevents SQL injection
Techniques et principes de détection
Fundamentals of in-depth learning -- a simple understanding of meta learning (from Li Hongyi's course notes)
Fundamentals of digital image processing (Gonzalez) I
Detection technology and principle
线性代数第三章-矩阵的初等变换与线性方程组
Pytorch learning record (7): skills in processing data and training models
線性代數第二章-矩陣及其運算
Unsupervised denoising - [tmi2022] ISCL: dependent self cooperative learning for unpaired image denoising
随机推荐
Integers have friends interval GCD + double pointer
Rainbow (DP)
線性代數第一章-行列式
PHP processing JSON_ Decode() parses JSON stringify
Failure to deliver XID in Seata distributed transaction project
Detection technology and principle
Kibana search syntax
线性代数第一章-行列式
Contrôle automatique (version Han min)
Advanced operation of idea debug
List segmentation best practices
Plane semi intersecting plate
MySQL advanced query
电机与拖动(戚金清版)学习整理
Custom exception class
The bottom implementation principle of thread - static agent mode
Explain of MySQL optimization
[leetcode 383] ransom letter
Mysql database foundation
檢測技術與原理