数组的新理解
数组可以具有多个维度。例如,下列声明创建一个四行两列的二维数组:
int[,] array = new int[4, 2];
另外,下列声明创建一个三维(4、2 和 3)数组:
int[, ,] array1 = new int[4, 2, 3];
数组初始化
可以在声明数组时将其初始化,如下例所示:
int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
int[, ,] array3D = new int[,,] { { { 1, 2, 3 } }, { { 4, 5, 6 } } };
也可以初始化数组但不指定级别:
int[,] array4 = { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
如果选择声明一个数组变量但不将其初始化,必须使用 new 运算符将一个数组分配给此变量。例如:
int[,] array5;
array5 = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } }; // OK
//array5 = {{1,2}, {3,4}, {5,6}, {7,8}}; // Error
也可以给数组元素赋值,例如:
array5[2,1] = 25;
int[,] array6 = new int[10, 10];
// Declare a single-dimensional array
int[] array1 = new int[5];
// Declare and set array element values
int[] array2 = new int[] { 1, 3, 5, 7, 9 };
// Alternative syntax
int[] array3 = { 1, 2, 3, 4, 5, 6 };
// Declare a two dimensional array
int[,] multiDimensionalArray1 = new int[2, 3];
// Declare and set array element values
int[,] multiDimensionalArray2 = { { 1, 2, 3 }, { 4, 5, 6 } };
// Declare a jagged array
int[][] jaggedArray = new int[6][];
// Set the values of the first array in the jagged array structure
jaggedArray[0] = new int[4] { 1, 2, 3, 4 };
int[] numbers = { 4, 5, 6, 1, 2, 3, -2, -1, 0 };
foreach (int i in numbers)
{
System.Console.WriteLine(i);
}
由于有了多维数组,可以使用相同方法来循环访问元素,例如:
int[,] numbers2D = new int[3, 2] { { 9, 99 }, { 3, 33 }, { 5, 55 } };
foreach (int i in numbers2D)
{
System.Console.Write("{0} ", i);
}
class ArrayTest
{
static void Main()
{
// Declare the array of two elements:
int[][] arr = new int[2][];
// Initialize the elements:
arr[0] = new int[5] { 1, 3, 5, 7, 9 };
arr[1] = new int[4] { 2, 4, 6, 8 };
// Display the array elements:
for (int i = 0; i < arr.Length; i++)
{
System.Console.Write("Element({0}): ", i);
for (int j = 0; j < arr[i].Length; j++)
{
System.Console.Write("{0}{1}", arr[i][j], j == (arr[i].Length - 1) ? "" : " ");
}
System.Console.WriteLine();
}
}
using System;
class DeclareArraysSample
{
public static void Main()
{
// 一维数组
int[] numbers = new int[5];
// 多维数组
string[,] names = new string[5,4];
// 数组的数组(交错数组)
byte[][] scores = new byte[5][];
// 创建交错数组
for (int i = 0; i < scores.Length; i++)
{
scores[i] = new byte[i+3];
}
// 打印每行的长度
for (int i = 0; i < scores.Length; i++)
{
Console.WriteLine("Length of row {0} is {1}", i, scores[i].Length);
}
}
}
string[] AreaArray =areacode.Split('|');
quhaostr= AreaArray[2].ToString();
if(MyArray==null)
{
string [] MyArray=null;
MyArray = new string[10]{"3G广东","3G上海","3G河南","3G浙江","3G福建","3G山东","3G江苏","3G湖南","3G广西","3G北京"};
}
for(int i = 0; i<10; i++)
{
if( MyArray[i].IndexOf(quhaostr)>0)
{
diqu = i+1;
break;
}
}
int[,] array = new int[4, 2];
另外,下列声明创建一个三维(4、2 和 3)数组:
int[, ,] array1 = new int[4, 2, 3];
数组初始化
可以在声明数组时将其初始化,如下例所示:
int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
int[, ,] array3D = new int[,,] { { { 1, 2, 3 } }, { { 4, 5, 6 } } };
也可以初始化数组但不指定级别:
int[,] array4 = { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
如果选择声明一个数组变量但不将其初始化,必须使用 new 运算符将一个数组分配给此变量。例如:
int[,] array5;
array5 = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } }; // OK
//array5 = {{1,2}, {3,4}, {5,6}, {7,8}}; // Error
也可以给数组元素赋值,例如:
array5[2,1] = 25;
int[,] array6 = new int[10, 10];
// Declare a single-dimensional array
int[] array1 = new int[5];
// Declare and set array element values
int[] array2 = new int[] { 1, 3, 5, 7, 9 };
// Alternative syntax
int[] array3 = { 1, 2, 3, 4, 5, 6 };
// Declare a two dimensional array
int[,] multiDimensionalArray1 = new int[2, 3];
// Declare and set array element values
int[,] multiDimensionalArray2 = { { 1, 2, 3 }, { 4, 5, 6 } };
// Declare a jagged array
int[][] jaggedArray = new int[6][];
// Set the values of the first array in the jagged array structure
jaggedArray[0] = new int[4] { 1, 2, 3, 4 };
int[] numbers = { 4, 5, 6, 1, 2, 3, -2, -1, 0 };
foreach (int i in numbers)
{
System.Console.WriteLine(i);
}
由于有了多维数组,可以使用相同方法来循环访问元素,例如:
int[,] numbers2D = new int[3, 2] { { 9, 99 }, { 3, 33 }, { 5, 55 } };
foreach (int i in numbers2D)
{
System.Console.Write("{0} ", i);
}
class ArrayTest
{
static void Main()
{
// Declare the array of two elements:
int[][] arr = new int[2][];
// Initialize the elements:
arr[0] = new int[5] { 1, 3, 5, 7, 9 };
arr[1] = new int[4] { 2, 4, 6, 8 };
// Display the array elements:
for (int i = 0; i < arr.Length; i++)
{
System.Console.Write("Element({0}): ", i);
for (int j = 0; j < arr[i].Length; j++)
{
System.Console.Write("{0}{1}", arr[i][j], j == (arr[i].Length - 1) ? "" : " ");
}
System.Console.WriteLine();
}
}
using System;
class DeclareArraysSample
{
public static void Main()
{
// 一维数组
int[] numbers = new int[5];
// 多维数组
string[,] names = new string[5,4];
// 数组的数组(交错数组)
byte[][] scores = new byte[5][];
// 创建交错数组
for (int i = 0; i < scores.Length; i++)
{
scores[i] = new byte[i+3];
}
// 打印每行的长度
for (int i = 0; i < scores.Length; i++)
{
Console.WriteLine("Length of row {0} is {1}", i, scores[i].Length);
}
}
}
string[] AreaArray =areacode.Split('|');
quhaostr= AreaArray[2].ToString();
if(MyArray==null)
{
string [] MyArray=null;
MyArray = new string[10]{"3G广东","3G上海","3G河南","3G浙江","3G福建","3G山东","3G江苏","3G湖南","3G广西","3G北京"};
}
for(int i = 0; i<10; i++)
{
if( MyArray[i].IndexOf(quhaostr)>0)
{
diqu = i+1;
break;
}
}