📄 declarearrays.cs
字号:
//版权所有 (C) 2000 Microsoft Corporation。保留所有权利。
/***************************************************
数组概述
C# 数组从零开始建立索引,即数组索引从零开始。C# 中数组的工作方式与在大多数其他流行语言中的工作方式类似。
但还有一些差异应引起注意。
声明数组时,方括号 ([]) 必须跟在类型后面,而不是标识符后面。在 C# 中,将方括号放在标识符后是不合法的语法。
int[] table; // not int table[];
另一细节是,数组的大小不是其类型的一部分,而在 C 语言中它却是数组类型的一部分。
这使您可以声明一个数组并向它分配 int 对象的任意数组,而不管数组长度如何。
int[] numbers; // declare numbers as an int array of any size
numbers = new int[10]; // numbers is a 10-element array
numbers = new int[20]; // now it's a 20-element array
声明数组
C# 支持一维数组、多维数组(矩形数组)和数组的数组(交错的数组)。
下面的示例展示如何声明不同类型的数组:
一维数组:
int[] numbers;
多维数组:
string[,] names; //二维数组
names = new string[3,2]; //3×2二维数组
string[,,] names; //三维数组
names = new string[2,3,4]; //2×3×4三维数组
数组的数组(交错的):
byte[][] scores;
声明数组(如上所示)并不实际创建它们。在 C# 中,数组是对象,必须进行实例化。
下面的示例展示如何创建数组:
一维数组:
int[] numbers = new int[5];
多维数组:
string[,] names = new string[5,4];
数组的数组(交错的):
byte[][] scores = new byte[5][];
for (int x = 0; x < scores.Length; x++)
{
scores[x] = new byte[4];
}
还可以有更大的数组。例如,可以有三维的矩形数组:
int[,,] buttons = new int[4,5,3];
甚至可以将矩形数组和交错数组混合使用。例如,下面的代码声明了类型为 int 的二维数组的三维数组的一维数组。
int[][,,][,] numbers;
*****************************************************************/
using System;
class DeclareArraysSample
{
void test()
{
// 交错数组,2行
string [][] str1 = new string[2][];
str1[0] = new string[] {"123abcd"};
str1[0] = new string[1] {"123abcd"};
str1[1] = new string[] {"cdef","fgh"};
str1[1] = new string[2] {"cdef","fgh"};
string [][] str2 = new string[2][] {
new string[] {"123abcd"},
new string[] {"cdef","fgh"}
};
//交错数组(两行二维数组)
int[][,] myArray1 = new int[2][,];
myArray1[0] = new int[3,2] { {1,2},{3,4},{5,6} }; //3行2列
myArray1[1] = new int[2,3] { {7,8,9},{10,11,12} }; //2行3列
// 一维数组
int [] numbers1 = new int[10] {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
int [] numbers2 = new int[ ] {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
int [] numbers3 = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
//二维数组,3行4列
int[,] numbers4 = new int[3, 4] {{1,2,3,4}, {5,6,7,8}, {9,10,11,12}};
int[,] numbers5 = new int[, ] {{1,2,3,4}, {5,6,7,8}, {9,10,11,12}};
int[,] numbers6 = {{1,2,3,4},{5,6,7,8}, {9,10,11,12}};
//三维数组,2×2×1
int[,,] numbers7 = new int[2, 2,1] { {{1},{2}},{{3},{4}}};
int[,,] numbers8 = new int[,,] { {{1},{2}},{{3},{4}}};
int[,,] numbers9 = { {{1},{2}},{{3},{4}}};
}
public static void Main()
{
// 一维数组
int[] numbers = new int[5];
// 多维数组,5行4列二维数组
string[,] names = new string[5,4];
// 数组的数组(交错数组)
byte[][] scores = new byte[5][];
// 创建交错数组
// Length表示 Array 的所有维数中元素的总数。
for (int i = 0, k = 1; i < scores.Length; i++)
{
scores[i] = new byte[i+3];
for (int j = 0; j < i+3; j++)
{
scores[i][j] = (byte)k; k++;
}
}
// 打印每一行及其长度
Console.WriteLine("交错数组,5行");
for (int i = 0; i < scores.Length; i++)
{
for (int j = 0; j < scores[i].Length; j++)
Console.Write(scores[i][j] + " ");
Console.WriteLine();
Console.WriteLine("Length of row {0} is {1}", i, scores[i].Length);
}
Console.WriteLine("交错数组,2行");
/***
string [][] str1 = new string[2][];
str1[0] = new string[] {"123abcd"}; // str1[0] = new string[1] {"abc"};
str1[1] = new string[] {"cdeff","fgh"}; // str1[1] = new string[2] {"cde","fgh"};
***/ //与下列等效
string [][] str1 = new string[2][] {
new string[] {"abc"},
new string[] {"cde","fgh"}
};
// 打印每一行及其长度
for (int i = 0; i < str1.Length; i++)
{
for (int j = 0; j < str1[i].Length; j++)
Console.Write(str1[i][j] + " ");
Console.WriteLine();
Console.WriteLine("Length of row {0} is {1}", i, str1[i].Length);
}
Console.WriteLine("一维数组,10个元素");
//int [] numbers1 = new int[10] {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; // 等效于下一句
int[] numbers1 = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
foreach (int i in numbers1)
{
Console.Write(i + " ");
}
Console.WriteLine();
Console.WriteLine("numbers1[2]={0}",numbers1[2]);
Console.WriteLine("二维数组,3行4列");
//二维数组,3行4列
int[,] numbers2 = new int[3, 4] {{1,2,3,4},
{5,6,7,8},
{9,10,11,12}}; //与下一句等效
/***
int[,] numbers2 = {{1,2,3,4},
{5,6,7,8},
{9,10,11,12}};
***/
foreach(int i in numbers2)
Console.Write("{0} ", i);
Console.WriteLine();
Console.WriteLine("*******************");
for (int i = 0; i <= numbers2.GetUpperBound(0); i++) //第一维
{
for (int j = 0; j <= numbers2.GetUpperBound(1); j++) //第二维
Console.Write("{0,2} ",numbers2[i,j]);
Console.WriteLine();
}
Console.WriteLine("三维数组,2×3×4");
//三维数组,2×3×4
int[,,] numbers3 = new int [2,3,4] { {
{1,2,3,4}, {5,6,7,8}, {9,10,11,12}
},
{
{13,14,15,16},{17,18,19,20},{21,22,23,24}
}
}; //与下一句等效
/**
int[,,] numbers3 = { {
{1,2,3,4}, {5,6,7,8}, {9,10,11,12}
},
{
{13,14,15,16},{17,18,19,20},{21,22,23,24}
}
};
**/
foreach(int i in numbers3)
Console.Write("{0} ", i);
Console.WriteLine();
Console.WriteLine("*******************");
for (int i = 0; i <= numbers3.GetUpperBound(0); i++) //第一维
{
Console.WriteLine("{");
for (int j = 0; j <= numbers3.GetUpperBound(1); j++) //第二维
{
Console.Write("{ ");
for (int k = 0; k <= numbers3.GetUpperBound(2); k++) //第三维
Console.Write("{0,2} ",numbers3[i,j,k]);
Console.Write("} ");
}
Console.WriteLine();
Console.WriteLine("}");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -