数组的新理解

数组可以具有多个维度。例如,下列声明创建一个四行两列的二维数组:

int[,] array = new int[42];

另外,下列声明创建一个三维(
42 和 3)数组: 

int[, ,] array1 = new int[423];

数组初始化
可以在声明数组时将其初始化,如下例所示:

int[,] array2D = new int[,] { { 12 }, { 34 }, { 56 }, { 78 } };
int[, ,] array3D = new int[,,] { { { 123 } }, { { 456 } } };
 

也可以初始化数组但不指定级别:

int[,] array4 = { { 12 }, { 34 }, { 56 }, { 78 } };


如果选择声明一个数组变量但不将其初始化,必须使用 
new 运算符将一个数组分配给此变量。例如:

int[,] array5;
array5 
= new int[,] { { 12 }, { 34 }, { 56 }, { 78 } };   // OK
//array5 = {{1,2}, {3,4}, {5,6}, {7,8}};   // Error
 
也可以给数组元素赋值,例如:

array5[
2,1= 25;

int[,] array6 = new int[1010];


// Declare a single-dimensional array 
int[] array1 = new int[5];

// Declare and set array element values
int[] array2 = new int[] { 13579 };

// Alternative syntax
int[] array3 = { 123456 };

// Declare a two dimensional array
int[,] multiDimensionalArray1 = new int[23];

// Declare and set array element values
int[,] multiDimensionalArray2 = { { 123 }, { 456 } };

// 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] { 1234 };


int[] numbers = { 456123-2-10 };
foreach (int i in numbers)
{
    System.Console.WriteLine(i);
}

由于有了多维数组,可以使用相同方法来循环访问元素,例如:

int[,] numbers2D = new int[32] { { 999 }, { 333 }, { 555 } };
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] { 13579 };
        arr[
1= new int[4] { 2468 };

        
// 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;
    }
}

posted @ 2007-03-16 09:29  wenanry  阅读(256)  评论(0编辑  收藏  举报