当前位置:网站首页>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
边栏推荐
- Customized communication between threads (reentrantlock)
- Collection and map thread safety problem solving
- Consistent hash algorithm used for redis cache load balancing
- JDBC connection database
- Usage scenario of copyonwritearraylist
- Complete example demonstration of creating table to page - joint table query
- 12. Monkeys climb mountains
- Plane semi intersecting plate
- Pytorch learning record (V): back propagation + gradient based optimizer (SGD, adagrad, rmsporp, Adam)
- St table template
猜你喜欢
自动控制原理知识点整合归纳(韩敏版)
Complete example demonstration of creating table to page - joint table query
Chapter 3 of linear algebra - Elementary Transformation of matrix and system of linear equations
深入理解去噪论文——FFDNet和CBDNet中noise level与噪声方差之间的关系探索
Pyqy5 learning (2): qmainwindow + QWidget + qlabel
Kibana search syntax
Algèbre linéaire chapitre 2 - matrice et son fonctionnement
自動控制(韓敏版)
Understanding and installing MySQL
JDBC connection database
随机推荐
Pytorch notes - complete code for linear regression & manual or automatic calculation of gradient code comparison
卡尔曼滤波与惯性组合导航
Detection technology and principle
线性代数第三章-矩阵的初等变换与线性方程组
Delete and truncate
Create binary tree
Contrôle automatique (version Han min)
[leetcode 67] sum of two binary numbers
Customized communication between threads (reentrantlock)
Fact final variable and final variable
Integration and induction of knowledge points of automatic control principle (Han min version)
自动控制(韩敏版)
POJ - 2955 brackets interval DP
@Problems caused by internal dead loop of postconstruct method
Programming training
[leetcode 383] ransom letter
Fundamentals of digital image processing (Gonzalez) II: gray transformation and spatial filtering
2. Devops sonar installation
Chapter 4 of line generation - linear correlation of vector systems
Optional best practices